Skip to content

Commit cc5329d

Browse files
authored
Merge pull request #4 from MattTheCuber/vue3-fix
Update editor to use modelValue for Vue 3
2 parents adc54bb + 34b443d commit cc5329d

File tree

3 files changed

+26
-7
lines changed

3 files changed

+26
-7
lines changed

example/read-only/viewer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
# Trame setup
99
# -----------------------------------------------------------------------------
1010

11-
server = get_server()
11+
server = get_server(client_type="vue2")
1212
state, ctrl = server.state, server.controller
1313

1414

trame_code/widgets/code.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
"""Module compatible with vue2 and vue3. To use it, you need to install **trame-code**"""
2+
23
from trame_client.widgets.core import AbstractElement
4+
35
from .. import module
46

57
__all__ = [
@@ -23,6 +25,7 @@ class Editor(HtmlElement):
2325
2426
:param options:
2527
:param value:
28+
:param model_value:
2629
:param theme:
2730
:param language:
2831
:param textmate:
@@ -41,6 +44,7 @@ def __init__(self, **kwargs):
4144
self._attr_names += [
4245
"options",
4346
"value",
47+
("model_value", "modelValue"),
4448
"theme",
4549
"language",
4650
"textmate",

vue-components/src/components/Editor.js

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ import * as monaco from "monaco-editor";
99
export default {
1010
name: "VSEditor",
1111
props: {
12+
modelValue: {
13+
type: String,
14+
},
1215
value: {
1316
type: String,
14-
default: "",
1517
},
1618
options: {
1719
type: Object,
@@ -34,8 +36,13 @@ export default {
3436
},
3537
},
3638
watch: {
39+
modelValue(v) {
40+
if (this.editor && v !== undefined && this.editor.getValue() !== v) {
41+
this.editor.setValue(v);
42+
}
43+
},
3744
value(v) {
38-
if (this.editor) {
45+
if (this.editor && v !== undefined && this.editor.getValue() !== v) {
3946
this.editor.setValue(v);
4047
}
4148
},
@@ -102,7 +109,7 @@ export default {
102109
}
103110

104111
this.editor = monaco.editor.create(this.$el, {
105-
value: this.value,
112+
value: this.modelValue || this.value,
106113
language: this.language,
107114
theme: this.theme,
108115
...this.options,
@@ -112,9 +119,17 @@ export default {
112119
provider.injectCSS();
113120
}
114121

115-
this.editor.onDidChangeModelContent(() =>
116-
this.$emit("input", this.editor.getValue())
117-
);
122+
this.lastValue = this.editor.getValue();
123+
124+
this.editor.onDidChangeModelContent(() => {
125+
const newValue = this.editor.getValue();
126+
if (this.lastValue === newValue) {
127+
return;
128+
}
129+
this.lastValue = newValue;
130+
this.$emit("update:modelValue", newValue);
131+
this.$emit("input", newValue);
132+
});
118133
},
119134
beforeUnmount() {
120135
this.editor.dispose();

0 commit comments

Comments
 (0)