Skip to content

Commit

Permalink
slate: finally found a better way to not mess up the cursor when doin…
Browse files Browse the repository at this point in the history
…g line level autoformat
  • Loading branch information
williamstein committed Apr 11, 2021
1 parent ca9bf69 commit 243f631
Showing 1 changed file with 6 additions and 9 deletions.
15 changes: 6 additions & 9 deletions src/smc-webapp/editors/slate/format/insert-text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import { markdown_to_slate } from "../markdown-to-slate";
import { applyOperations } from "../operations";
import { slateDiff } from "../slate-diff";
import { getRules } from "../elements";
import { moveCursorToEndOfElement } from "../control";
import { ReactEditor } from "../slate-react";
import { SlateEditor } from "../editable-markdown";
import { formatHeading, setSelectionAndFocus } from "./commands";
Expand All @@ -53,7 +52,7 @@ export const withInsertText = (editor) => {
type: "link",
isInline: true,
url: text,
children: [{ text}],
children: [{ text }],
},
]);
return;
Expand Down Expand Up @@ -250,17 +249,15 @@ function markdownAutoformatAt(
focusEditorAt(editor, new_cursor);
} else {
// **NON-INLINE CASE**
// Select what is being replaced so it will get deleted when the
// insert happens.
Transforms.select(editor, {
anchor: { path, offset: start == -1 ? 0 : start },
focus: { path, offset: Math.max(0, node.text.length) },
});
// Remove the node with the text that we're autoformatting
// so the new doc replaces it. NOTE that doing this works
// **much** better than selecting the corresponding text
// and letting insertNodes take care of it.
Transforms.removeNodes(editor, { at: path });
// We put an empty paragraph after, so that formatting
// is preserved (otherwise it gets stripped); also some documents
// ending in void block elements are difficult to use.
Transforms.insertNodes(editor, doc);
moveCursorToEndOfElement(editor, doc[0]);

// Normally just move the cursor beyond what was just
// inserted, though sometimes it makes more sense to
Expand Down

0 comments on commit 243f631

Please sign in to comment.