Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
GuilhermeF03 committed Jun 8, 2024
1 parent 5ba0007 commit fc5d4d4
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 35 deletions.
17 changes: 13 additions & 4 deletions code/client/src/domain/editor/fugue/Fugue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,15 @@ export class Fugue {
*/
reviveLocal(selection: Selection): ReviveOperation[] {
const nodes = Array.from(this.traverseBySelection(selection, true));
return nodes.map(node => this.reviveNode(node.id));
return nodes.map(node => {
if (node.value === '\n') {
selection.start.line++;
selection.start.column = 0;
}
else selection.start.column++;

return this.reviveNode(node.id, selection.start)
});
}

/**
Expand All @@ -179,16 +187,17 @@ export class Fugue {
*/
reviveLocalByCursor(cursor: Cursor) {
const node = this.getNodeByCursor(cursor);
if (node) return this.reviveNode(node.id);
if (node) return this.reviveNode(node.id, cursor);
}

/**
* Revives a node based on the given id
* @param id
* @param cursor
*/
reviveNode(id: Id): ReviveOperation {
reviveNode(id: Id, cursor : Cursor): ReviveOperation {
this.tree.reviveNode(id);
return { type: 'revive', id };
return { type: 'revive', id, cursor };
}

/**
Expand Down
23 changes: 23 additions & 0 deletions code/client/src/domain/editor/slate/utils/selection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,29 @@ export function pointToCursor(editor: Editor, point: Point): Cursor {
return cursor;
}

export const cursorToPoint = (editor : Editor, cursor: Cursor): Point => {
const { line, column } = cursor;
const path = [line];
let offset = column;

const nodes = Array.from(Node.children(editor, [line]));

for (const [node, nodePath] of nodes) {
if(!Text.isText(node)) continue;
const text = node as Text;
if (offset <= text.text.length) {
path.push(...nodePath);
break;
}
offset -= text.text.length;
}

return { path, offset };
}




/**
* Returns the selection by range
* @param editor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function Editor({ title, fugue, communication }: SlateEditorProps) {
const fugueOperations = getFugueOperations(fugue);
const { cursors } = useCursors(communication);
const { renderElement, renderLeaf } = useRenderers(editor, fugue, communication);
const decorate = useDecorate(editor, cursors);
//const decorate = useDecorate(editor, cursors);

const updateEditor = useCallback(
(newValue: Descendant[]) => {
Expand Down Expand Up @@ -94,7 +94,7 @@ function Editor({ title, fugue, communication }: SlateEditorProps) {
spellCheck={false}
renderElement={renderElement}
renderLeaf={renderLeaf}
decorate={decorate}
//decorate={decorate}
onDragStart={e => e.preventDefault()}
onDOMBeforeInput={onInput}
onCut={onCut}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import useSocketListeners from '@services/communication/socket/useSocketListener
import { type Operation } from '@notespace/shared/src/document/types/operations';
import { Communication } from '@services/communication/communication';
import { FugueDomainOperations } from '@domain/editor/fugue/operations/fugue/types';
import { Editor, Transforms, Selection } from 'slate';
import {Editor, Transforms, Location} from 'slate';
import { Cursor } from '@domain/editor/cursor';
import {cursorToPoint, getSelection} from "@domain/editor/slate/utils/selection";


/**
* Hook client socket listeners to events
Expand All @@ -20,41 +22,31 @@ function useEvents(
) {
function onOperation(operations: Operation[]) {
fugueOperations.applyOperations(operations);
const {start: selectionStart, end: selectionEnd} = getSelection(editor);

operations.forEach((op: Operation) => {
if (['insert', 'delete', 'revive'].includes(op.type)) {
const { cursor } = op as Operation & { cursor: Cursor };
const currSelection = editor.selection;

if (currSelection) {
const { anchor, focus } = currSelection;
const newStart = { ...anchor };
const newEnd = { ...focus };
if(cursor.line !== selectionEnd.line) return;
if(cursor.column > selectionEnd.column) return;

// Update the cursor position
const delta = ['insert', 'revive'].includes(op.type) ? 1 : -1;

if (cursor.line === anchor.path[0]) {
if (op.type === 'insert') {
if (cursor.column <= anchor.offset) {
newStart.offset += op.value.length;
}
if (cursor.column <= focus.offset) {
newEnd.offset += op.value.length;
}
} else if (op.type === 'delete') {
if (cursor.column < anchor.offset) {
newStart.offset = Math.max(anchor.offset - 1, cursor.column);
}
if (cursor.column < focus.offset) {
newEnd.offset = Math.max(focus.offset - 1, cursor.column);
}
}
}
const newSelection: Selection = {
anchor: newStart,
focus: newEnd,
};
Transforms.select(editor, newSelection);
selectionEnd.column += delta;
if(delta > 0) { // If we are deleting, we want to anchor the start of the selection
selectionStart.column += delta;
}
}

const newSelection : Location = {
anchor: cursorToPoint(editor, selectionStart),
focus: cursorToPoint(editor, selectionEnd),
};

console.log("New selection", newSelection);
Transforms.select(editor, newSelection);
});
onDone();
}
Expand Down
2 changes: 1 addition & 1 deletion code/shared/src/document/types/operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export type BlockStyleOperation = {
export type ReviveOperation = {
type: "revive";
id: Id;
// cursor: Cursor;
cursor: Cursor;
};

export type Operation =
Expand Down

0 comments on commit fc5d4d4

Please sign in to comment.