Skip to content

Commit

Permalink
[WIP] Migrate to CodeMirror
Browse files Browse the repository at this point in the history
  • Loading branch information
xingrz committed Mar 25, 2024
1 parent 1881340 commit 44ce567
Show file tree
Hide file tree
Showing 15 changed files with 230 additions and 256 deletions.
133 changes: 127 additions & 6 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"lint": "eslint --ext .ts,.vue src"
},
"dependencies": {
"brace": "^0.11.1",
"codemirror": "^6.0.1",
"md5": "^2.3.0",
"naive-ui": "^2.38.1",
"pinia": "^2.1.7",
Expand Down
13 changes: 10 additions & 3 deletions src/components/BSMap.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<template>
<div :class="$style.map" :style="style">
<BSRow v-for="(row, index) in rows" :key="index" :content="row" :row="index" />
<BSRow v-for="({ row, offset }, index) in rows" :key="index" :content="row" :row="index" :offset="offset" />
</div>
</template>

Expand All @@ -18,11 +18,18 @@ const props = defineProps<{
size: number;
}>();
const rows = computed(() => props.content.split('\n'));
const rows = computed(() => {
let offset = 0;
return props.content.split('\n').map((row) => {
const o = offset;
offset += row.length + 1;
return { row, offset: o };
})
});
const cols = computed(() => {
let cols = 0;
for (const row of rows.value) {
for (const { row } of rows.value) {
const c = Math.max(cols, row.split('\\').length);
if (c > cols) cols = c;
}
Expand Down
2 changes: 0 additions & 2 deletions src/components/BSMap/BSCell.vue
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import BSIcon from './BSIcon.vue';
const props = defineProps<{
content: string;
row: number;
offset: number;
}>();
Expand All @@ -41,7 +40,6 @@ const style = computed(() => ({
function handleClick(): void {
editorStore.selection = {
row: props.row,
offset: props.offset,
length: props.content.length,
};
Expand Down
10 changes: 5 additions & 5 deletions src/components/BSMap/BSRow.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
<div :class="$style.cells">
<BSCell v-for="({ cell, offset }, index) in cells" :key="index"
:class="{ [$style.selection]: true, [$style.focused]: isFocused(row, offset, cell.length) }" :content="cell"
:row="row" :offset="offset" />
:offset="offset" />
</div>
<div :class="$style.texts">
<BSText v-for="({ text, offset, align }, index) in texts" :key="index"
:class="{ [$style.selection]: true, [$style.focused]: isFocused(row, offset, text.length) }" :content="text"
:align="align" :row="row" :offset="offset" />
:align="align" :offset="offset" />
</div>
</div>
</template>
Expand All @@ -24,12 +24,13 @@ import BSText from './BSText.vue';
const props = defineProps<{
content: string;
row: number;
offset: number;
}>();
const editorStore = useEditorStore();
const parts = computed(() => {
let offset = 0;
let offset = props.offset;
return props.content.split('~~').map((part) => {
const o = offset;
offset += part.length + 2;
Expand All @@ -38,7 +39,7 @@ const parts = computed(() => {
});
const cells = computed(() => {
let offset = 0;
let offset = props.offset;
return parts.value[0].part.split('\\').map((cell) => {
const o = offset;
offset += cell.length + 1;
Expand All @@ -57,7 +58,6 @@ function isFocused(row: number, offset: number, length: number): boolean {
const { selection } = editorStore;
return (
typeof selection != 'undefined' &&
selection.row == row &&
selection.offset >= offset &&
selection.offset <= offset + length
);
Expand Down
2 changes: 0 additions & 2 deletions src/components/BSMap/BSText.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,13 @@ import { useEditorStore } from '@/stores/editor';
const props = defineProps<{
content: string;
align: number;
row: number;
offset: number;
}>();
const editorStore = useEditorStore();
function handleClick(): void {
editorStore.selection = {
row: props.row,
offset: props.offset,
length: props.content.length,
};
Expand Down
Loading

0 comments on commit 44ce567

Please sign in to comment.