Skip to content

Commit

Permalink
Fixed Slate Text Selection
Browse files Browse the repository at this point in the history
  • Loading branch information
R1c4rdCo5t4 committed Mar 28, 2024
1 parent 2349077 commit 2b6700c
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 27 deletions.
2 changes: 1 addition & 1 deletion code/client/dev-dist/sw.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ define(['./workbox-fda11f75'], (function (workbox) { 'use strict';
"revision": "3ca0b8505b4bec776b69afdba2768812"
}, {
"url": "index.html",
"revision": "0.0e2rp9avb9o"
"revision": "0.965t3hopnrg"
}], {});
workbox.cleanupOutdatedCaches();
workbox.registerRoute(new workbox.NavigationRoute(workbox.createHandlerBoundToURL("index.html"), {
Expand Down
8 changes: 4 additions & 4 deletions code/client/src/editor/crdt/fugue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,14 +155,14 @@ export class Fugue {
) {
inBounds = true;
}
// yield node if in bounds
if (inBounds && node.value !== '\n') {
yield node;
}
// end condition
if (lineCounter === end.line && columnCounter === end.column) {
break;
}
// yield node if in bounds
if (inBounds && node.value !== '\n') {
yield node;
}
// update counters
if (node.value === '\n') {
lineCounter++;
Expand Down
2 changes: 1 addition & 1 deletion code/client/src/editor/slate.js/model/CustomEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const CustomEditor = {
const isActive = CustomEditor.isMarkActive(editor, mark);
Editor.addMark(editor, mark, !isActive);

const selection = getSelection(editor)!;
const selection = getSelection(editor);
fugue.updateStyleLocal(selection, !isActive, mark);
},
};
Expand Down
63 changes: 42 additions & 21 deletions code/client/src/editor/slate.js/utils/selection.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,63 @@
import { Editor, Range, Point } from 'slate';
import { Editor, Range, Point, Text, Node, Path } from 'slate';
import { Cursor, Selection } from '../model/cursor.ts';

function emptySelection(): Selection {
return {
start: { line: 0, column: 0 },
end: { line: 0, column: 0 },
};
}

export function isSelected(editor: Editor) {
if (!editor.selection) return false;
const { anchor, focus } = editor.selection;
return anchor.path !== focus.path && anchor.offset !== focus.offset;
}

// export function getSelection(editor: Editor): Selection {
// if (!editor.selection) return { start: { line: 0, column: 0 }, end: { line: 0, column: 0 } };
// const { anchor, focus } = editor.selection;
// const { path: startLine, offset: startColumn } = focus;
// const { path: endLine, offset: endColumn } = anchor;
// const start = { line: startLine[0], column: startColumn };
// const end = { line: endLine[0], column: endColumn };
// const isRightToLeft = start.line > end.line || (start.line === end.line && start.column > end.column);
// return isRightToLeft ? { start: end, end: start } : { start, end };
// }

// does the same as the function above
export function getSelection(editor: Editor): Selection {
const { selection } = editor;
if (!selection) {
return {
start: { line: 0, column: 0 },
end: { line: 0, column: 0 },
};
return emptySelection();
}
const [start, end] = Range.edges(selection);
return {
start: pointToCursor(start),
end: pointToCursor(end),
start: pointToCursor(editor, start),
end: pointToCursor(editor, end),
};
}

function pointToCursor(point: Point): Cursor {
function pointToCursor(editor: Editor, point: Point): Cursor {
return {
line: point.path[0],
column: point.offset,
column: getAbsoluteOffset(editor, point),
};
}

function getAbsoluteOffset(editor: Editor, point: Point): number {
let offset = 0;
let line = 0;

// Traverse the nodes in the document
for (const [node, path] of Node.nodes(editor)) {
if (Text.isText(node)) {
if (Path.isBefore(path, point.path)) {
// If the current text node is before the point, add its length to the offset
offset += node.text.length;
// If the text node contains a line break, increment the line count and reset the offset
if (node.text.includes('\n')) {
line++;
offset = 0;
}
} else if (Path.equals(path, point.path)) {
// If the current text node contains the point, add the point's offset to the total offset
offset += point.offset;
break;
}
}
if (line !== point.path[0]) {
offset = 0;
}
}

return offset;
}

0 comments on commit 2b6700c

Please sign in to comment.