Skip to content

Commit

Permalink
Fixed delete selection on input handler
Browse files Browse the repository at this point in the history
  • Loading branch information
GuilhermeF03 committed Jun 11, 2024
1 parent 8a6325a commit 49995af
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 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 @@ -118,7 +118,7 @@ export class Fugue {
*/
deleteLocal(selection: Selection): DeleteOperation[] {
const nodes = Array.from(this.traverseBySelection(selection));
const cursor = selection.start;
const cursor = {...selection.start}
return nodes.map(node => {
if (node.value === '\n') {
cursor.line++;
Expand Down
9 changes: 7 additions & 2 deletions code/client/src/domain/editor/slate/operations/input.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Editor } from 'slate';
import { ReactEditor } from 'slate-react';
import CustomEditor from '@domain/editor/slate/CustomEditor';
import { isEqual } from 'lodash';
import {isEqual, min} from 'lodash';
import { getKeyFromInputEvent } from '@domain/editor/slate/utils/domEvents';
import { getSelection, isSelected } from '@domain/editor/slate/utils/selection';
import { Cursor, emptyCursor } from '@domain/editor/cursor';
Expand All @@ -20,9 +20,13 @@ export default (editor: Editor, connector: InputConnector, onFormat: (mark: Inli
if (!key) return;

const selection = getSelection(editor);
const cursor = selection.start;
console.log("Selection: ", selection)
const cursor = min([{...selection.start}, {...selection.end}]) as Cursor; // always use the start of the selection
console.log("Initial Cursor: ", cursor);
// if there is a selection, delete the selected text
if (isSelected(editor)) connector.deleteSelection(selection);
console.log("Selection after deletion: ", getSelection(editor));
console.log("Cursor after deletion: ", cursor);
switch (key) {
case 'Enter':
onEnter(cursor);
Expand Down Expand Up @@ -77,6 +81,7 @@ export default (editor: Editor, connector: InputConnector, onFormat: (mark: Inli
*/
function onKey(key: string, cursor: Cursor) {
const styles = CustomEditor.getMarks(editor) as InlineStyle[];
console.log("Cursor: ", cursor)
connector.insertCharacter(key, cursor, styles);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ function Editor({ title, connectors, fugue }: EditorProps) {

const syncEditor = useCallback(
(slate?: Descendant[]) => {
console.log("Syncing editor...")
const newSlate = slate || toSlate(fugue);
updateEditor(newSlate);
},
Expand Down

0 comments on commit 49995af

Please sign in to comment.