|
4 | 4 | const dispatch = createEventDispatcher(); |
5 | 5 |
|
6 | 6 | import { monacoEditorLoad } from './scripts/monaco-loader'; |
7 | | - export let modelValue = ''; |
8 | | - export let rightValue = ''; |
| 7 | + export let code = ''; |
| 8 | + export let rightCode = ''; |
9 | 9 | export let language = 'json'; |
10 | 10 | export let readOnly = false; |
11 | 11 | export let diffEditor = false; |
|
15 | 15 | let rootElement, editor, setLeftValue, setRightValue, setLeftMarkers, setRightMarkers; |
16 | 16 | $: { |
17 | 17 | if (setLeftValue) { |
18 | | - setLeftValue(modelValue); |
| 18 | + setLeftValue(code); |
19 | 19 | } |
20 | 20 | } |
21 | 21 | $: { |
22 | 22 | if (setRightValue) { |
23 | | - setRightValue(rightValue); |
| 23 | + setRightValue(rightCode); |
24 | 24 | } |
25 | 25 | } |
26 | 26 | $: { |
|
37 | 37 | onMount(async () => { |
38 | 38 | const monaco = await monacoEditorLoad; |
39 | 39 | const options = { |
40 | | - value: modelValue, |
| 40 | + value: code, |
41 | 41 | readOnly, |
42 | 42 | theme: 'vs-dark', |
43 | 43 | language, |
|
59 | 59 | originalEditable: true, |
60 | 60 | ...options |
61 | 61 | }); |
62 | | - const original = monaco.editor.createModel(modelValue, language); |
63 | | - const modified = monaco.editor.createModel(rightValue, language); |
| 62 | + const original = monaco.editor.createModel(code, language); |
| 63 | + const modified = monaco.editor.createModel(rightCode, language); |
64 | 64 | const leftEditor = editor.getOriginalEditor(); |
65 | 65 | const rightEditor = editor.getModifiedEditor(); |
66 | 66 | rightEditor.updateOptions({ readOnly: true }); |
67 | 67 | editor.setModel({ original, modified }); |
68 | 68 | original.onDidChangeContent(() => { |
69 | 69 | const value = original.getValue(); |
70 | | - modelValue = value; |
| 70 | + code = value; |
71 | 71 | }); |
72 | 72 |
|
73 | | - setLeftValue = (modelValue) => { |
| 73 | + setLeftValue = (code) => { |
74 | 74 | const value = original.getValue(); |
75 | | - if (modelValue !== value) { |
76 | | - original.setValue(modelValue); |
| 75 | + if (code !== value) { |
| 76 | + original.setValue(code); |
77 | 77 | } |
78 | 78 | }; |
79 | | - setRightValue = (modelValue) => { |
| 79 | + setRightValue = (code) => { |
80 | 80 | const value = modified.getValue(); |
81 | | - if (modelValue !== value) { |
82 | | - modified.setValue(modelValue); |
| 81 | + if (code !== value) { |
| 82 | + modified.setValue(code); |
83 | 83 | } |
84 | 84 | }; |
85 | 85 | setLeftMarkers = (markers) => { |
|
95 | 95 | editor = monaco.editor.create(rootElement, options); |
96 | 96 | editor.onDidChangeModelContent(() => { |
97 | 97 | const value = editor.getValue(); |
98 | | - modelValue = value; |
| 98 | + code = value; |
99 | 99 | }); |
100 | 100 | editor.onDidChangeCursorPosition((evt) => { |
101 | 101 | dispatch('changeCursorPosition', evt); |
102 | 102 | }); |
103 | 103 | editor.onDidFocusEditorText((evt) => { |
104 | 104 | dispatch('focusEditorText', evt); |
105 | 105 | }); |
106 | | - setLeftValue = (modelValue) => { |
| 106 | + setLeftValue = (code) => { |
107 | 107 | const value = editor.getValue(); |
108 | | - if (modelValue !== value) { |
109 | | - editor.setValue(modelValue); |
| 108 | + if (code !== value) { |
| 109 | + editor.setValue(code); |
110 | 110 | } |
111 | 111 | }; |
112 | 112 | setRightValue = () => { |
|
0 commit comments