diff --git a/frontend/package.json b/frontend/package.json index 9bac09b6..605169c6 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -16,7 +16,7 @@ "github-markdown-css": "^4.0.0", "highlight.js": "^10.4.1", "katex": "^0.6.0", - "loadsh": "^0.0.4", + "lodash": "^4.17.21", "markdown-it": "^12.0.1", "markdown-it-attrs": "^3.0.3", "markdown-it-footnote": "^3.0.2", diff --git a/frontend/src/components/Editor.vue b/frontend/src/components/Editor.vue index a532f4ba..c69c6180 100644 --- a/frontend/src/components/Editor.vue +++ b/frontend/src/components/Editor.vue @@ -45,7 +45,6 @@ export default defineComponent({ const getEditor = () => refEditor.value const revealLine = (line: number) => getEditor().revealLine(line) const revealLineInCenter = (line: number) => getEditor().revealLineInCenter(line) - const switchTodo = (line: number, checked: boolean) => getEditor().switchTodo(line, checked) const toUri = (file: any) => File.toUri(file) async function inputPassword (title: string, filename: string) { @@ -253,100 +252,35 @@ export default defineComponent({ const resize = () => nextTick(() => getEditor().resize()) const insert = (text?: string) => getEditor().insert(text) + const replaceValue = (value?: { search: string; replace: string }) => { if (value) { getEditor().replaceValue(value.search, value.replace) } } - const toggleWrap = () => getEditor().toggleWrap() - - const editTableCell = async (params?: { start: number; end: number; cellIndex: number }) => { - if (!params) { - return + const replaceLine = (params?: { line: number; value: string }) => { + if (params) { + getEditor().replaceLine(params.line, params.value) } - - const { start, end, cellIndex } = params - if (end - start !== 1) { - toast.show('warning', '暂只支持编辑单行文本') - return - } - - const escapedSplit = (str: string) => { - const result = [] - const max = str.length - let pos = 0 - let ch = str.charCodeAt(pos) - let isEscaped = false - let lastPos = 0 - let current = '' - - while (pos < max) { - if (ch === 0x7c/* | */) { - if (!isEscaped) { - // pipe separating cells, '|' - result.push(current + str.substring(lastPos, pos)) - current = '' - lastPos = pos + 1 - } else { - // escaped pipe, '\|' - current += str.substring(lastPos, pos - 1) - lastPos = pos - } - } - - isEscaped = (ch === 0x5c/* \ */) - pos++ - - ch = str.charCodeAt(pos) - } - - result.push(current + str.substring(lastPos)) - - if (result.length && result[0] === '') result.shift() - if (result.length && result[result.length - 1] === '') result.pop() - - return result - } - - const text = getEditor().getLineContent(start).trim() - const columns = escapedSplit(text) - const cellText = columns[cellIndex] - - if (typeof cellText !== 'string') { - toast.show('warning', '编辑错误') - return - } - - let value = await modal.input({ - title: '编辑单元格', - type: 'textarea', - value: cellText, - modalWidth: '600px', - hint: '单元格内容', - }) - if (typeof value !== 'string') { - toast.show('warning', '取消编辑') - return - } - - if (!value.startsWith(' ') && cellIndex > 0) value = ' ' + value - if (!value.endsWith(' ') && cellIndex < columns.length - 1) value += ' ' - columns[cellIndex] = value.replace(/\|/g, '\\|').replace(/\n/g, ' ') - - let content = columns.join('|').trim() - if (text.startsWith('|')) content = '| ' + content - if (text.endsWith('|')) content += ' |' - - getEditor().replaceLine(start, content) } + const getLine = (params?: {line: number; callback?: (value: string) => void}) => { + if (params) { + const { line, callback } = params + callback && callback(getEditor().getLineContent(line)) + } + } + + const toggleWrap = () => getEditor().toggleWrap() + onMounted(() => { bus.on('resize', resize) bus.on('editor-insert-value', insert) bus.on('editor-replace-value', replaceValue) + bus.on('editor-get-line', getLine) + bus.on('editor-replace-line', replaceLine) bus.on('editor-toggle-wrap', toggleWrap) - bus.on('editor-edit-table-cell', editTableCell) bus.on('file-new', createFile as any) restartTimer() }) @@ -355,16 +289,16 @@ export default defineComponent({ bus.off('resize', resize) bus.off('editor-insert-value', insert) bus.off('editor-replace-value', replaceValue) + bus.off('editor-get-line', getLine) + bus.off('editor-replace-line', replaceLine) bus.off('editor-toggle-wrap', toggleWrap) bus.off('file-new', createFile as any) - bus.off('editor-edit-table-cell', editTableCell) }) return { refEditor, revealLine, revealLineInCenter, - switchTodo, setCurrentValue, syncScrollView, pasteImg, diff --git a/frontend/src/components/MonacoEditor.vue b/frontend/src/components/MonacoEditor.vue index ae3d492d..67d2b867 100644 --- a/frontend/src/components/MonacoEditor.vue +++ b/frontend/src/components/MonacoEditor.vue @@ -204,16 +204,6 @@ export default defineComponent({ return getEditor().getModel()!!.getLineContent(line) } - function switchTodo (line: number, checked: boolean) { - if (checked) { - const value = getLineContent(line).replace('[ ]', `[x] ~~${dayjs().format('YYYY-MM-DD HH:mm')}~~`) - replaceLine(line, value) - } else { - const value = getLineContent(line).replace(/(\[x\] ~~[\d-: ]+~~|\[x\])/, '[ ]') - replaceLine(line, value) - } - } - function getValue () { return getEditor().getModel()!!.getValue(getMonaco().editor.DefaultEndOfLine.LF) } @@ -323,7 +313,6 @@ export default defineComponent({ revealLine, revealLineInCenter, setScrollToTop, - switchTodo, replaceValue, replaceLine, toggleWrap, diff --git a/frontend/src/components/Preview.vue b/frontend/src/components/Preview.vue index a0c4516e..b0c13016 100644 --- a/frontend/src/components/Preview.vue +++ b/frontend/src/components/Preview.vue @@ -38,7 +38,7 @@