Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
R1c4rdCo5t4 committed Jun 18, 2024
1 parent dfd2848 commit 991f88f
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 24 deletions.
2 changes: 1 addition & 1 deletion code/client/src/domain/editor/fugue/Fugue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ export class Fugue {
* @param cursor
* @param returnDeleted
*/
getNodeByCursor({ line, column }: Cursor, returnDeleted : boolean = false): FugueNode | undefined {
getNodeByCursor({ line, column }: Cursor, returnDeleted: boolean = false): FugueNode | undefined {
//if (column === 0) return this.tree.getLineRoot(line);
if (line === 0 && column === 0) return this.tree.root;
const start = { line, column };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ function removeTextOperation(editor: Editor, operation: BaseRemoveTextOperation)
// Normalize selection to account for line root nodes
const start = pointToCursor(editor, { ...operation });


const end = {
line: start.line,
column: start.column + operation.text.length - 1,
Expand Down
53 changes: 35 additions & 18 deletions code/client/src/domain/editor/slate/utils/selection.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Editor, Node, Path, Point, Range, Text } from 'slate';
import { Cursor, emptyCursor, emptySelection, Selection } from '@domain/editor/cursor';
import { first, isEqual } from 'lodash';
import { CustomElement } from '@domain/editor/slate/types';

/**
* Checks if the current selection is active
Expand Down Expand Up @@ -38,16 +39,12 @@ const pointsToSelection = (editor: Editor, start: Point, end: Point): Selection
* Converts a slate point to a cursor
* @param editor
* @param point
* @param absolute
*/
export function pointToCursor(editor: Editor, point: Point, absolute : boolean = false): Cursor {
export function pointToCursor(editor: Editor, point: Point): Cursor {
const line = point.path[0];
const cursor: Cursor = { line, column: point.offset };

if (point.path[1] === 0) {
cursor.column = (absolute ? cursor.column : cursor.column + 1);
return cursor;
}
if (point.path[1] === 0) return cursor;

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

Expand All @@ -64,29 +61,49 @@ export function pointToCursor(editor: Editor, point: Point, absolute : boolean =
}
}

// Slate offset is off by one compared to the cursor column
cursor.column = (absolute ? cursor.column : cursor.column + 1);
return cursor;
}

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

const nodes = Array.from(Node.children(editor, [line]));
// Get the path to the line node
const linePath = [line];

// Check if the path exists in the editor
if (!Editor.hasPath(editor, linePath)) {
throw new Error(`Cannot find a node at line ${line}`);
}

// Get the node at the line path
const lineNode = Node.get(editor, linePath);

// Check if the node is a valid block or container node
if (!Editor.isBlock(editor, lineNode as CustomElement)) {
throw new Error(`Node at line ${line} is not a block node`);
}

// Traverse the children of the line node to find the correct text node
for (const [node, nodePath] of Node.children(editor, linePath)) {
if (Text.isText(node)) {
if (offset <= node.text.length) {
return { path: nodePath, offset };
}
offset -= node.text.length;
}
}

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;
// If the offset is not found, return the end of the line node
const lastTextNode = Node.last(editor, linePath);
if (lastTextNode) {
const [lastNode, lastPath] = lastTextNode;
if (Text.isText(lastNode)) {
return { path: lastPath, offset: lastNode.text.length };
}
offset -= text.text.length;
}

return { path, offset };
throw new Error('Cursor position is out of bounds');
};

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
describe('Undo cut', () => {
it('Should work', () => {
expect(1).toBe(1);
});
});
it('Should work', () => {
expect(1).toBe(1);
});
});

0 comments on commit 991f88f

Please sign in to comment.