From 991f88ff2c034d07d5e0545c8f324729fda27744 Mon Sep 17 00:00:00 2001 From: Ricardo Costa Date: Tue, 18 Jun 2024 18:23:51 +0100 Subject: [PATCH] Fixes --- code/client/src/domain/editor/fugue/Fugue.ts | 2 +- .../operations/history/toHistoryOperations.ts | 1 - .../domain/editor/slate/utils/selection.ts | 53 ++++++++++++------- .../history/text-tests/block-style.test.ts | 8 +-- 4 files changed, 40 insertions(+), 24 deletions(-) diff --git a/code/client/src/domain/editor/fugue/Fugue.ts b/code/client/src/domain/editor/fugue/Fugue.ts index bfe79438..0e7d562e 100644 --- a/code/client/src/domain/editor/fugue/Fugue.ts +++ b/code/client/src/domain/editor/fugue/Fugue.ts @@ -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 }; diff --git a/code/client/src/domain/editor/slate/operations/history/toHistoryOperations.ts b/code/client/src/domain/editor/slate/operations/history/toHistoryOperations.ts index c7f7e7ee..5c1c8ab8 100644 --- a/code/client/src/domain/editor/slate/operations/history/toHistoryOperations.ts +++ b/code/client/src/domain/editor/slate/operations/history/toHistoryOperations.ts @@ -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, diff --git a/code/client/src/domain/editor/slate/utils/selection.ts b/code/client/src/domain/editor/slate/utils/selection.ts index df2cfbdc..3aab86c6 100644 --- a/code/client/src/domain/editor/slate/utils/selection.ts +++ b/code/client/src/domain/editor/slate/utils/selection.ts @@ -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 @@ -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])); @@ -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'); }; /** diff --git a/code/client/tests/editor/slate/operations/history/text-tests/block-style.test.ts b/code/client/tests/editor/slate/operations/history/text-tests/block-style.test.ts index 28f7b228..9e17ddf9 100644 --- a/code/client/tests/editor/slate/operations/history/text-tests/block-style.test.ts +++ b/code/client/tests/editor/slate/operations/history/text-tests/block-style.test.ts @@ -1,5 +1,5 @@ describe('Undo cut', () => { - it('Should work', () => { - expect(1).toBe(1); - }); -}); \ No newline at end of file + it('Should work', () => { + expect(1).toBe(1); + }); +});