Skip to content

Commit

Permalink
Clean up unnecessary store setters
Browse files Browse the repository at this point in the history
  • Loading branch information
xingrz committed Mar 23, 2024
1 parent 0fcb389 commit 3101b7c
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 59 deletions.
38 changes: 5 additions & 33 deletions src/App.vue
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<template>
<resizable v-model:width="width">
<template v-slot:default>
<editor v-model:content="content" v-model:selection="selection" v-model:scroll="scroll" :icons="iconStore.icons"
:size="editorStore.size" />
<editor v-model:content="editorStore.content" v-model:selection="editorStore.selection"
v-model:scroll="editorStore.scroll" :icons="iconStore.icons" :size="editorStore.size" />
</template>
<template v-slot:fixed>
<scroller v-model:scroll="scroll">
<BSMap v-bind:content="content" v-bind:size="editorStore.size" />
<scroller v-model:scroll="editorStore.scroll">
<BSMap :content="editorStore.content" :size="editorStore.size" />
</scroller>
</template>
</resizable>
Expand All @@ -17,7 +17,6 @@ import { computed } from 'vue';
import { useEditorStore } from '@/stores/editor';
import { useIconStore } from '@/stores/icon';
import ISelection from '@/types/selection';
import Resizable from './components/Resizable.vue';
import Scroller from './components/Scroller.vue';
Expand All @@ -35,34 +34,7 @@ const width = computed({
return Math.max(Math.min(width, max), min);
},
set(v: number) {
editorStore.setWidth(v);
}
});
const scroll = computed({
get(): number {
return editorStore.scroll;
},
set(v: number) {
editorStore.setScroll(v);
}
});
const selection = computed({
get(): ISelection | null {
return editorStore.selection;
},
set(v: ISelection | null) {
editorStore.setSelection(v);
}
});
const content = computed({
get(): string {
return editorStore.content;
},
set(v: string) {
editorStore.save(v);
editorStore.width = v;
}
});
</script>
Expand Down
4 changes: 2 additions & 2 deletions src/components/BSMap/BSCell.vue
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ const icons = computed(() => {
});
function handleClick(): void {
editorStore.setSelection({
editorStore.selection = {
row: props.row,
offset: props.offset,
length: props.content.length,
from: 'preview',
});
};
}
function updateRatio(index: number, newRatio: number): void {
Expand Down
4 changes: 2 additions & 2 deletions src/components/BSMap/BSText.vue
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ const props = defineProps<{
const editorStore = useEditorStore();
function handleClick(): void {
editorStore.setSelection({
editorStore.selection = {
row: props.row,
offset: props.offset,
length: props.content.length,
from: 'preview',
});
};
}
</script>

Expand Down
36 changes: 14 additions & 22 deletions src/stores/editor.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { defineStore } from 'pinia';
import { ref } from 'vue';
import { ref, watch } from 'vue';
import { debounce } from 'radash';

import ISelection from '@/types/selection';
Expand All @@ -13,31 +13,23 @@ export const useEditorStore = defineStore('editor', () => {
const scroll = ref(0);
const selection = ref<ISelection | null>(null);

function setSize(value: number): void {
size.value = value;
debouncedSetItem('size', `${size.value}`);
}
watch(size, (value) => {
debouncedSetItem('split', String(value));
});

function setWidth(value: number): void {
width.value = value;
debouncedSetItem('width', `${width.value}`);
}
watch(width, (value) => {
debouncedSetItem('width', String(value));
});

function setScroll(value: number): void {
scroll.value = value;
}

function setSelection(value: ISelection | null): void {
selection.value = value;
}

function save(value: string): void {
content.value = value;
watch(content, (value) => {
debouncedSetItem('content', value);
}
});

return {
size, width, content, scroll, selection,
setSize, setWidth, setScroll, setSelection, save
size,
width,
content,
scroll,
selection,
};
});

0 comments on commit 3101b7c

Please sign in to comment.