mirror of
https://codeberg.org/Sevichecc/Seigwai.git
synced 2025-06-17 08:49:14 +08:00
Compare commits
5 commits
3544b30b60
...
45c32e307c
Author | SHA1 | Date | |
---|---|---|---|
45c32e307c | |||
9dfd5d34d9 | |||
d71878fc17 | |||
0dbfc7d16b | |||
c2a47f7159 |
8 changed files with 94 additions and 47 deletions
10
src/App.vue
10
src/App.vue
|
@ -1,13 +1,7 @@
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { MilkdownProvider } from '@milkdown/vue'
|
import EditorWrapper from './components/Milkdown/EditroWrapper.vue'
|
||||||
import { ProsemirrorAdapterProvider } from '@prosemirror-adapter/vue'
|
|
||||||
import Milkdown from './components/Milkdown/Editor.vue'
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<MilkdownProvider>
|
<EditorWrapper />
|
||||||
<ProsemirrorAdapterProvider>
|
|
||||||
<Milkdown />
|
|
||||||
</ProsemirrorAdapterProvider>
|
|
||||||
</MilkdownProvider>
|
|
||||||
</template>
|
</template>
|
||||||
|
|
|
@ -14,6 +14,7 @@ let tooltipProvider: BlockProvider | undefined
|
||||||
|
|
||||||
watch([loading], () => {
|
watch([loading], () => {
|
||||||
const editor = get()
|
const editor = get()
|
||||||
|
// eslint-disable-next-line antfu/if-newline
|
||||||
if (loading.value || !editor || tooltipProvider) return
|
if (loading.value || !editor || tooltipProvider) return
|
||||||
|
|
||||||
editor.action((ctx) => {
|
editor.action((ctx) => {
|
||||||
|
|
13
src/components/Milkdown/EditroWrapper.vue
Normal file
13
src/components/Milkdown/EditroWrapper.vue
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
<script setup lang="ts">
|
||||||
|
import { MilkdownProvider } from '@milkdown/vue'
|
||||||
|
import { ProsemirrorAdapterProvider } from '@prosemirror-adapter/vue'
|
||||||
|
import Editor from './Editor.vue'
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<template>
|
||||||
|
<MilkdownProvider>
|
||||||
|
<ProsemirrorAdapterProvider>
|
||||||
|
<Editor />
|
||||||
|
</ProsemirrorAdapterProvider>
|
||||||
|
</MilkdownProvider>
|
||||||
|
</template>
|
|
@ -1,20 +1,16 @@
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { editorViewCtx } from '@milkdown/core'
|
|
||||||
import { SlashProvider } from '@milkdown/plugin-slash'
|
import { SlashProvider } from '@milkdown/plugin-slash'
|
||||||
import { createCodeBlockCommand } from '@milkdown/preset-commonmark'
|
|
||||||
import { callCommand } from '@milkdown/utils'
|
|
||||||
import { useInstance } from '@milkdown/vue'
|
|
||||||
import { usePluginViewContext } from '@prosemirror-adapter/vue'
|
|
||||||
import { onMounted, onUnmounted, ref, watch } from 'vue'
|
import { onMounted, onUnmounted, ref, watch } from 'vue'
|
||||||
|
import { usePluginViewContext } from '@prosemirror-adapter/vue'
|
||||||
import type { VNodeRef } from 'vue'
|
import type { VNodeRef } from 'vue'
|
||||||
const { view, prevState } = usePluginViewContext()
|
import { useCommands } from '../../composables/commands'
|
||||||
const [loading, get] = useInstance()
|
|
||||||
|
|
||||||
const divRef = ref<VNodeRef>()
|
|
||||||
|
|
||||||
|
const { addCodeBlock, toggleBold } = useCommands()
|
||||||
let tooltipProvider: SlashProvider
|
let tooltipProvider: SlashProvider
|
||||||
|
|
||||||
|
const { view, prevState } = usePluginViewContext()
|
||||||
|
const divRef = ref<VNodeRef>()
|
||||||
|
|
||||||
onMounted(() => {
|
onMounted(() => {
|
||||||
tooltipProvider = new SlashProvider({
|
tooltipProvider = new SlashProvider({
|
||||||
content: divRef.value as any,
|
content: divRef.value as any,
|
||||||
|
@ -30,30 +26,21 @@ watch([view, prevState], () => {
|
||||||
onUnmounted(() => {
|
onUnmounted(() => {
|
||||||
tooltipProvider.destroy()
|
tooltipProvider.destroy()
|
||||||
})
|
})
|
||||||
|
|
||||||
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)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<div ref="divRef">
|
<div ref="divRef">
|
||||||
<button
|
<button
|
||||||
className="text-gray-600 bg-slate-200 px-2 py-1 rounded-lg hover:bg-slate-300 border hover:text-gray-900"
|
class="text-gray-600 bg-slate-200 px-2 py-1 rounded-lg hover:bg-slate-300 border hover:text-gray-900"
|
||||||
@mousedown="addCodeBlock"
|
@click="addCodeBlock"
|
||||||
>
|
>
|
||||||
Code Block
|
Code Block
|
||||||
</button>
|
</button>
|
||||||
|
<button
|
||||||
|
class="text-gray-600 bg-slate-200 px-2 py-1 rounded-lg hover:bg-slate-300 border hover:text-gray-900"
|
||||||
|
@click="toggleBold"
|
||||||
|
>
|
||||||
|
Bold
|
||||||
|
</button>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
|
@ -1,15 +1,14 @@
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { TooltipProvider } from '@milkdown/plugin-tooltip'
|
import { TooltipProvider } from '@milkdown/plugin-tooltip'
|
||||||
import { toggleStrongCommand } from '@milkdown/preset-commonmark'
|
|
||||||
import { callCommand } from '@milkdown/utils'
|
|
||||||
import { useInstance } from '@milkdown/vue'
|
|
||||||
import { usePluginViewContext } from '@prosemirror-adapter/vue'
|
import { usePluginViewContext } from '@prosemirror-adapter/vue'
|
||||||
import { onMounted, onUnmounted, ref, watch } from 'vue'
|
import { onMounted, onUnmounted, ref, watch } from 'vue'
|
||||||
|
|
||||||
import type { VNodeRef } from 'vue'
|
import type { VNodeRef } from 'vue'
|
||||||
|
import { useCommands } from '../../composables/commands'
|
||||||
|
|
||||||
|
const { toggleBold, toggleInlineCode, toggleItalic } = useCommands()
|
||||||
const { view, prevState } = usePluginViewContext()
|
const { view, prevState } = usePluginViewContext()
|
||||||
const [loading, get] = useInstance()
|
|
||||||
|
|
||||||
const divRef = ref<VNodeRef>()
|
const divRef = ref<VNodeRef>()
|
||||||
|
|
||||||
|
@ -30,21 +29,27 @@ watch([view, prevState], () => {
|
||||||
onUnmounted(() => {
|
onUnmounted(() => {
|
||||||
tooltipProvider.destroy()
|
tooltipProvider.destroy()
|
||||||
})
|
})
|
||||||
|
|
||||||
const toggleBold = (e: Event) => {
|
|
||||||
if (loading.value) return
|
|
||||||
e.preventDefault()
|
|
||||||
get()!.action(callCommand(toggleStrongCommand.key))
|
|
||||||
}
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<div ref="divRef">
|
<div ref="divRef">
|
||||||
<button
|
<button
|
||||||
className="text-gray-600 bg-slate-200 px-2 py-1 rounded-lg hover:bg-slate-300 border hover:text-gray-900"
|
class="text-gray-600 bg-slate-200 px-2 py-1 rounded-lg hover:bg-slate-300 border hover:text-gray-900"
|
||||||
@mousedown="toggleBold"
|
@click="toggleBold"
|
||||||
>
|
>
|
||||||
Bold
|
Bold
|
||||||
</button>
|
</button>
|
||||||
|
<button
|
||||||
|
class="text-gray-600 bg-slate-200 px-2 py-1 rounded-lg hover:bg-slate-300 border hover:text-gray-900"
|
||||||
|
@click="toggleInlineCode"
|
||||||
|
>
|
||||||
|
Code
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
class="text-gray-600 bg-slate-200 px-2 py-1 rounded-lg hover:bg-slate-300 border hover:text-gray-900"
|
||||||
|
@click="toggleItalic"
|
||||||
|
>
|
||||||
|
Italic
|
||||||
|
</button>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
47
src/composables/commands.ts
Normal file
47
src/composables/commands.ts
Normal file
|
@ -0,0 +1,47 @@
|
||||||
|
/* eslint-disable antfu/if-newline */
|
||||||
|
import { useInstance } from '@milkdown/vue'
|
||||||
|
import {
|
||||||
|
createCodeBlockCommand,
|
||||||
|
toggleEmphasisCommand,
|
||||||
|
toggleInlineCodeCommand,
|
||||||
|
toggleStrongCommand,
|
||||||
|
} from '@milkdown/preset-commonmark'
|
||||||
|
import { callCommand } from '@milkdown/utils'
|
||||||
|
import { editorViewCtx } from '@milkdown/core'
|
||||||
|
|
||||||
|
export const useCommands = () => {
|
||||||
|
const [loading, get] = useInstance()
|
||||||
|
|
||||||
|
const toggleBold = (e: Event) => {
|
||||||
|
if (loading.value) return
|
||||||
|
e.preventDefault()
|
||||||
|
get()!.action(callCommand(toggleStrongCommand.key))
|
||||||
|
}
|
||||||
|
|
||||||
|
const toggleInlineCode = (e: Event) => {
|
||||||
|
if (loading.value) return
|
||||||
|
e.preventDefault()
|
||||||
|
get()!.action(callCommand(toggleInlineCodeCommand.key))
|
||||||
|
}
|
||||||
|
|
||||||
|
const toggleItalic = (e: Event) => {
|
||||||
|
if (loading.value) return
|
||||||
|
e.preventDefault()
|
||||||
|
get()!.action(callCommand(toggleEmphasisCommand.key))
|
||||||
|
}
|
||||||
|
|
||||||
|
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 { toggleBold, addCodeBlock, toggleInlineCode, toggleItalic }
|
||||||
|
}
|
Loading…
Reference in a new issue