Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(editor): Fix code node’s content property to be reactive #6931

Merged
merged 1 commit into from
Aug 15, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions packages/editor-ui/src/components/CodeNodeEditor/CodeNodeEditor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,16 @@ export default defineComponent({
mode(newMode, previousMode: CodeExecutionMode) {
this.reloadLinter();

if (this.content.trim() === CODE_PLACEHOLDERS[this.language]?.[previousMode]) {
if (
this.getCurrentEditorContent().trim() === CODE_PLACEHOLDERS[this.language]?.[previousMode]
) {
this.refreshPlaceholder();
}
},
language(newLanguage, previousLanguage: CodeNodeEditorLanguage) {
if (this.content.trim() === CODE_PLACEHOLDERS[previousLanguage]?.[this.mode]) {
if (
this.getCurrentEditorContent().trim() === CODE_PLACEHOLDERS[previousLanguage]?.[this.mode]
) {
this.refreshPlaceholder();
}

Expand All @@ -100,11 +104,6 @@ export default defineComponent({
},
computed: {
...mapStores(useRootStore),
content(): string {
if (!this.editor) return '';

return this.editor.state.doc.toString();
},
placeholder(): string {
return CODE_PLACEHOLDERS[this.language]?.[this.mode] ?? '';
},
Expand All @@ -120,6 +119,9 @@ export default defineComponent({
},
},
methods: {
getCurrentEditorContent() {
return this.editor?.state.doc.toString() ?? '';
},
onMouseOver(event: MouseEvent) {
const fromElement = event.relatedTarget as HTMLElement;
const ref = this.$refs.codeNodeEditorContainer as HTMLDivElement | undefined;
Expand Down Expand Up @@ -151,7 +153,7 @@ export default defineComponent({
if (!this.editor) return;

this.editor.dispatch({
changes: { from: 0, to: this.content.length, insert: this.placeholder },
changes: { from: 0, to: this.getCurrentEditorContent().length, insert: this.placeholder },
});
},
line(lineNumber: number): Line | null {
Expand All @@ -166,7 +168,7 @@ export default defineComponent({

if (lineNumber === 'final') {
this.editor.dispatch({
selection: { anchor: (this.modelValue ?? this.content).length },
selection: { anchor: (this.modelValue ?? this.getCurrentEditorContent()).length },
});
return;
}
Expand All @@ -187,7 +189,7 @@ export default defineComponent({
try {
// @ts-ignore - undocumented fields
const { fromA, toB } = viewUpdate?.changedRanges[0];
const full = this.content.slice(fromA, toB);
const full = this.getCurrentEditorContent().slice(fromA, toB);
const lastDotIndex = full.lastIndexOf('.');

let context = null;
Expand Down