From d71878fc17ba4153839e0fd8cf67c163806a2efd Mon Sep 17 00:00:00 2001 From: Seviche Date: Fri, 24 Mar 2023 21:46:31 +0800 Subject: [PATCH] Refactor commands --- src/components/Milkdown/Slash.vue | 11 ++++++++- src/components/Milkdown/Tooltip.vue | 25 +++++++++++++------ src/composables/quickCommands.ts | 38 +++++++++++++++++++++++++++++ src/utils/types.ts | 0 src/utils/utils.ts | 31 ----------------------- 5 files changed, 66 insertions(+), 39 deletions(-) create mode 100644 src/composables/quickCommands.ts delete mode 100644 src/utils/types.ts delete mode 100644 src/utils/utils.ts diff --git a/src/components/Milkdown/Slash.vue b/src/components/Milkdown/Slash.vue index 9b890a1..b566ad5 100644 --- a/src/components/Milkdown/Slash.vue +++ b/src/components/Milkdown/Slash.vue @@ -3,7 +3,7 @@ import { SlashProvider } from '@milkdown/plugin-slash' import { onMounted, onUnmounted, ref, watch } from 'vue' import { usePluginViewContext } from '@prosemirror-adapter/vue' import type { VNodeRef } from 'vue' -import { addCodeBlock } from '../../utils/utils' +import { useAddCodeBlock, useToggleBold } from '../../composables/quickCommands' let tooltipProvider: SlashProvider @@ -25,6 +25,9 @@ watch([view, prevState], () => { onUnmounted(() => { tooltipProvider.destroy() }) + +const { addCodeBlock } = useAddCodeBlock() +const { toggleBold } = useToggleBold() diff --git a/src/components/Milkdown/Tooltip.vue b/src/components/Milkdown/Tooltip.vue index fbe0d17..4b5eae3 100644 --- a/src/components/Milkdown/Tooltip.vue +++ b/src/components/Milkdown/Tooltip.vue @@ -1,15 +1,17 @@ diff --git a/src/composables/quickCommands.ts b/src/composables/quickCommands.ts new file mode 100644 index 0000000..f5d8afa --- /dev/null +++ b/src/composables/quickCommands.ts @@ -0,0 +1,38 @@ +/* eslint-disable antfu/if-newline */ +import { useInstance } from '@milkdown/vue' +import { + createCodeBlockCommand, + toggleStrongCommand, +} from '@milkdown/preset-commonmark' +import { callCommand } from '@milkdown/utils' +import { editorViewCtx } from '@milkdown/core' + +export const useToggleBold = () => { + const [loading, get] = useInstance() + + const toggleBold = (e: Event) => { + if (loading.value) return + e.preventDefault() + get()!.action(callCommand(toggleStrongCommand.key)) + } + + return { toggleBold } +} + +export const useAddCodeBlock = () => { + const [loading, get] = useInstance() + + const addCodeBlock = (e: Event) => { + if (loading.value) return + e.preventDefault() + get()!.action((ctx) => { + const view = ctx.get(editorViewCtx) + const { dispatch, state } = view + const { tr, selection } = state + const { from } = selection + dispatch(tr.deleteRange(from - 1, from)) + return callCommand(createCodeBlockCommand.key)(ctx) + }) + } + return { addCodeBlock } +} diff --git a/src/utils/types.ts b/src/utils/types.ts deleted file mode 100644 index e69de29..0000000 diff --git a/src/utils/utils.ts b/src/utils/utils.ts deleted file mode 100644 index 0c806d7..0000000 --- a/src/utils/utils.ts +++ /dev/null @@ -1,31 +0,0 @@ -/* eslint-disable antfu/if-newline */ -import { useInstance } from '@milkdown/vue' -import { editorViewCtx } from '@milkdown/core' -import { callCommand } from '@milkdown/utils' -import { - createCodeBlockCommand, - toggleStrongCommand, -} from '@milkdown/preset-commonmark' - -export const addCodeBlock = (e: Event) => { - const [loading, get] = useInstance() - if (loading.value) return - e.preventDefault() - - get()!.action((ctx) => { - const view = ctx.get(editorViewCtx) - const { dispatch, state } = view - const { tr, selection } = state - const { from } = selection - dispatch(tr.deleteRange(from - 1, from)) - return callCommand(createCodeBlockCommand.key)(ctx) - }) -} - -export const toggleBold = (e: Event) => { - const [loading, get] = useInstance() - if (loading.value) return - e.preventDefault() - - get()!.action(callCommand(toggleStrongCommand.key)) -}