|
5 | 5 | closeBracketsKeymap,
|
6 | 6 | completionKeymap,
|
7 | 7 | } from "@codemirror/autocomplete";
|
8 |
| - import { indentWithTab } from "@codemirror/commands"; |
| 8 | + import { defaultKeymap, indentWithTab } from "@codemirror/commands"; |
9 | 9 | import { markdown } from "@codemirror/lang-markdown";
|
10 | 10 | import {
|
11 | 11 | bracketMatching,
|
| 12 | + defaultHighlightStyle, |
12 | 13 | foldKeymap,
|
13 | 14 | indentOnInput,
|
| 15 | + syntaxHighlighting, |
14 | 16 | syntaxTree,
|
15 | 17 | } from "@codemirror/language";
|
16 | 18 | import { languages } from "@codemirror/language-data";
|
17 | 19 | import { highlightSelectionMatches, searchKeymap } from "@codemirror/search";
|
18 | 20 | import { EditorState } from "@codemirror/state";
|
19 | 21 | import {
|
20 | 22 | crosshairCursor,
|
| 23 | + drawSelection, |
21 | 24 | dropCursor,
|
22 | 25 | EditorView,
|
23 | 26 | highlightActiveLine,
|
| 27 | + highlightSpecialChars, |
24 | 28 | keymap,
|
25 | 29 | rectangularSelection,
|
26 | 30 | } from "@codemirror/view";
|
27 | 31 | import { printTree } from "@lezer-unofficial/printer";
|
28 | 32 | import { GFM } from "@lezer/markdown";
|
29 |
| - import { minimalSetup } from "codemirror"; |
30 | 33 | import { hypermdExtensions, hypermdMarkdownExtensions } from "hypermd";
|
| 34 | + import { LoroExtensions } from "loro-codemirror"; |
| 35 | + import { Awareness, type LoroDoc, UndoManager } from "loro-crdt"; |
31 | 36 | import CodeMirror from "svelte-codemirror-editor";
|
| 37 | + import { uint8ArrayToBase64 } from "uint8array-extras"; |
32 | 38 |
|
33 | 39 | export interface Props {
|
34 |
| - value: string; |
| 40 | + doc: LoroDoc; |
35 | 41 | }
|
36 | 42 |
|
37 |
| - let { value = $bindable() }: Props = $props(); |
| 43 | + let { doc }: Props = $props(); |
| 44 | +
|
| 45 | + const awareness: Awareness = new Awareness(doc.peerIdStr); |
| 46 | + const undoManager = new UndoManager(doc, {}); |
38 | 47 |
|
39 | 48 | const extensions = [
|
40 |
| - minimalSetup, |
| 49 | + // minimalSetup, |
| 50 | +
|
| 51 | + highlightSpecialChars(), |
| 52 | + drawSelection(), |
| 53 | + syntaxHighlighting(defaultHighlightStyle, { fallback: true }), |
| 54 | + keymap.of([...defaultKeymap]), |
| 55 | +
|
41 | 56 | dropCursor(),
|
42 | 57 | EditorState.allowMultipleSelections.of(true),
|
43 | 58 | indentOnInput(),
|
|
49 | 64 | highlightActiveLine(),
|
50 | 65 | highlightSelectionMatches(),
|
51 | 66 |
|
| 67 | + LoroExtensions( |
| 68 | + doc, |
| 69 | + { |
| 70 | + awareness, |
| 71 | + user: { colorClassName: "user1", name: "User 1" }, |
| 72 | + }, |
| 73 | + undoManager, |
| 74 | + ), |
| 75 | +
|
52 | 76 | EditorView.lineWrapping,
|
53 | 77 | hypermdExtensions,
|
54 | 78 | keymap.of([
|
|
69 | 93 | printTree(syntaxTree(view.state), view.state.doc.toString()),
|
70 | 94 | );
|
71 | 95 |
|
| 96 | + return true; |
| 97 | + }, |
| 98 | + }, |
| 99 | + { |
| 100 | + key: "Cmd-Shift-s", |
| 101 | + run: () => { |
| 102 | + if (import.meta.env.PROD) return true; |
| 103 | +
|
| 104 | + console.debug(uint8ArrayToBase64(doc.export({ mode: "snapshot" }))); |
| 105 | +
|
72 | 106 | return true;
|
73 | 107 | },
|
74 | 108 | },
|
|
83 | 117 | codeLanguages: languages,
|
84 | 118 | extensions: [GFM, hypermdMarkdownExtensions],
|
85 | 119 | })}
|
86 |
| - bind:value |
| 120 | + value={/* This sets it initially, but Loro handles further updates. */ |
| 121 | + doc.getText("codemirror").toString()} |
87 | 122 | />
|
0 commit comments