diff --git a/.git-branches.toml b/.git-branches.toml index 59529c1ef92..909943b063d 100644 --- a/.git-branches.toml +++ b/.git-branches.toml @@ -2,7 +2,7 @@ [branches] main = "main" -perennials = ["main"] +perennials = ["main", "v6"] perennial-regex = "" [create] diff --git a/.github/workflows/town.yml b/.github/workflows/town.yml index 6a4ecf0221f..412c67f7ce1 100644 --- a/.github/workflows/town.yml +++ b/.github/workflows/town.yml @@ -23,6 +23,6 @@ jobs: with: fetch-depth: 0 - - uses: git-town/action@v1.2.0 + - uses: git-town/action@v1.2.1 with: history-limit: 50 diff --git a/packages/crepe/src/feature/toolbar/component.tsx b/packages/crepe/src/feature/toolbar/component.tsx index 9c14a511336..2104ba02a50 100644 --- a/packages/crepe/src/feature/toolbar/component.tsx +++ b/packages/crepe/src/feature/toolbar/component.tsx @@ -3,10 +3,12 @@ import type { Selection } from '@milkdown/kit/prose/state' import { Icon } from '@milkdown/kit/component' import { toggleLinkCommand } from '@milkdown/kit/component/link-tooltip' -import { commandsCtx, editorViewCtx } from '@milkdown/kit/core' +import { commandsCtx, editorCtx, EditorStatus } from '@milkdown/kit/core' import { emphasisSchema, inlineCodeSchema, + isMarkSelectedCommand, + isNodeSelectedCommand, linkSchema, strongSchema, toggleEmphasisCommand, @@ -17,7 +19,6 @@ import { strikethroughSchema, toggleStrikethroughCommand, } from '@milkdown/kit/preset/gfm' -import { findNodeInSelection } from '@milkdown/kit/prose' import { MarkType, type NodeType } from '@milkdown/kit/prose/model' import clsx from 'clsx' import { defineComponent, type Ref, type ShallowRef, h, Fragment } from 'vue' @@ -79,32 +80,19 @@ export const Toolbar = defineComponent({ ctx && fn(ctx) } - const isMarkActive = (mark: MarkType) => { - if (!ctx) return false - const { state } = ctx.get(editorViewCtx) - if (!state) return false - const { doc, selection } = state - return doc.rangeHasMark(selection.from, selection.to, mark) - } - - const isInlineNodeActive = (node: NodeType) => { - if (!ctx) return false - const state = ctx.get(editorViewCtx).state - if (!state) return false - - const result = findNodeInSelection(ctx.get(editorViewCtx).state, node) - return result.hasNode - } - function isActive(type: NodeType | MarkType) { // make sure the function subscribed to vue reactive props.selection.value + // Check if the edtior is ready + const status = ctx.get(editorCtx).status + if (status !== EditorStatus.Created) return false + const commands = ctx.get(commandsCtx) if (type instanceof MarkType) { - return isMarkActive(type) + return commands.call(isMarkSelectedCommand.key, type) } - return isInlineNodeActive(type) + return commands.call(isNodeSelectedCommand.key, type) } const flags = useCrepeFeatures(ctx).get() diff --git a/packages/plugins/preset-commonmark/src/commands/index.ts b/packages/plugins/preset-commonmark/src/commands/index.ts new file mode 100644 index 00000000000..9583544c127 --- /dev/null +++ b/packages/plugins/preset-commonmark/src/commands/index.ts @@ -0,0 +1,23 @@ +import type { MarkType, NodeType } from '@milkdown/prose/model' + +import { findNodeInSelection } from '@milkdown/prose' +import { $command } from '@milkdown/utils' + +/// A command to check if a mark is selected. +export const isMarkSelectedCommand = $command('IsMarkSelected', () => { + return (markType?: MarkType) => (state) => { + if (!markType) return false + const { doc, selection } = state + const hasLink = doc.rangeHasMark(selection.from, selection.to, markType) + return hasLink + } +}) + +/// A command to check if a node is selected. +export const isNodeSelectedCommand = $command('IsNoteSelected', () => { + return (nodeType?: NodeType) => (state) => { + if (!nodeType) return false + const result = findNodeInSelection(state, nodeType) + return result.hasNode + } +}) diff --git a/packages/plugins/preset-commonmark/src/composed/commands.ts b/packages/plugins/preset-commonmark/src/composed/commands.ts index b4cb1df41a2..dec26aac6c9 100644 --- a/packages/plugins/preset-commonmark/src/composed/commands.ts +++ b/packages/plugins/preset-commonmark/src/composed/commands.ts @@ -1,5 +1,6 @@ import type { MilkdownPlugin } from '@milkdown/ctx' +import { isMarkSelectedCommand, isNodeSelectedCommand } from '../commands' import { toggleEmphasisCommand, toggleInlineCodeCommand, @@ -51,4 +52,7 @@ export const commands: MilkdownPlugin[] = [ toggleLinkCommand, updateLinkCommand, + + isMarkSelectedCommand, + isNodeSelectedCommand, ] diff --git a/packages/plugins/preset-commonmark/src/index.ts b/packages/plugins/preset-commonmark/src/index.ts index 5614988581a..f3c7ead1960 100644 --- a/packages/plugins/preset-commonmark/src/index.ts +++ b/packages/plugins/preset-commonmark/src/index.ts @@ -13,6 +13,7 @@ export * from './node' export * from './mark' export * from './plugin' export * from './composed' +export * from './commands' /// The commonmark preset, includes all the plugins. export const commonmark: MilkdownPlugin[] = [