Skip to content

Commit

Permalink
Make use of @vueuse/core
Browse files Browse the repository at this point in the history
  • Loading branch information
xingrz committed Apr 14, 2024
1 parent b28d20f commit 2849284
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 60 deletions.
89 changes: 89 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"lint": "eslint --ext .ts,.vue src"
},
"dependencies": {
"@vueuse/core": "^10.9.0",
"ace-code": "^1.32.8",
"md5": "^2.3.0",
"pinia": "^2.1.7",
Expand Down
7 changes: 3 additions & 4 deletions src/components/Editor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ import {
defineModel,
onBeforeUnmount,
onMounted,
watch,
ref,
useCssModule,
} from 'vue';
import { useResizeObserver } from '@vueuse/core';
import { Ace, edit as aceEdit, config as aceConfig } from 'ace-code';
import 'ace-code/src/ext/language_tools';
import 'ace-code/styles/ace.css';
Expand All @@ -26,7 +27,6 @@ import * as RDT from '@/editor/rdt';
import type ISelection from '@/types/selection';
import type IIcon from '@/types/icon';
import useClientSize from '@/composables/useClientSize';
import bindEditorValue from '@/composables/bindEditorValue';
import bindEditorSelection from '@/composables/bindEditorSelection';
import bindEditorScroll from '@/composables/bindEditorScroll';
Expand Down Expand Up @@ -70,8 +70,7 @@ onBeforeUnmount(() => {
editor.value = undefined;
});
const holderSize = useClientSize(holder);
watch(holderSize, () => editor.value?.resize());
useResizeObserver(holder, () => editor.value?.resize());
class Completer implements Ace.Completer {
private style = useCssModule();
Expand Down
5 changes: 2 additions & 3 deletions src/composables/bindEditorScroll.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { watch, type Ref } from 'vue';
import { whenever } from '@vueuse/core';
import type { Ace } from 'ace-code';

import onRefAssigned from './onRefAssigned';

export default function bindEditorScroll(editor: Ref<Ace.Editor | undefined>, scroll: Ref<number | undefined>): void {
onRefAssigned(editor, (value) => {
whenever(editor, (value) => {
const session = value.getSession();
session.on('changeScrollTop', () => {
const scrollTop = session.getScrollTop();
Expand Down
5 changes: 2 additions & 3 deletions src/composables/bindEditorSelection.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { watch, type Ref } from 'vue';
import { whenever } from '@vueuse/core';
import type { Ace } from 'ace-code';

import onRefAssigned from './onRefAssigned';

import type ISelection from '@/types/selection';

interface Renderer extends Ace.VirtualRenderer {
Expand All @@ -11,7 +10,7 @@ interface Renderer extends Ace.VirtualRenderer {
}

export default function bindEditorSelection(editorRef: Ref<Ace.Editor | undefined>, selection: Ref<ISelection | undefined>): void {
onRefAssigned(editorRef, (editor) => {
whenever(editorRef, (editor) => {
applyRange(editor, toRange({ row: 0, offset: 0, length: 0 }));

editor.selection.on('changeCursor', () => {
Expand Down
5 changes: 2 additions & 3 deletions src/composables/bindEditorValue.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import type { Ref } from 'vue';
import { whenever } from '@vueuse/core';
import type { Ace } from 'ace-code';

import onRefAssigned from './onRefAssigned';

export default function bindEditorValue(editor: Ref<Ace.Editor | undefined>, val: Ref<string | undefined>): void {
onRefAssigned(editor, (value) => {
whenever(editor, (value) => {
if (val.value) {
value.setValue(val.value);
}
Expand Down
9 changes: 0 additions & 9 deletions src/composables/onRefAssigned.ts

This file was deleted.

9 changes: 0 additions & 9 deletions src/composables/onRefUnassigned.ts

This file was deleted.

29 changes: 0 additions & 29 deletions src/composables/useClientSize.ts

This file was deleted.

0 comments on commit 2849284

Please sign in to comment.