Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .git-branches.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[branches]
main = "main"
perennials = ["main"]
perennials = ["main", "v6"]
perennial-regex = ""

[create]
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/town.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
30 changes: 9 additions & 21 deletions packages/crepe/src/feature/toolbar/component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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'
Expand Down Expand Up @@ -79,32 +80,19 @@ export const Toolbar = defineComponent<ToolbarProps>({
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()
Expand Down
23 changes: 23 additions & 0 deletions packages/plugins/preset-commonmark/src/commands/index.ts
Original file line number Diff line number Diff line change
@@ -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
}
})
4 changes: 4 additions & 0 deletions packages/plugins/preset-commonmark/src/composed/commands.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { MilkdownPlugin } from '@milkdown/ctx'

import { isMarkSelectedCommand, isNodeSelectedCommand } from '../commands'
import {
toggleEmphasisCommand,
toggleInlineCodeCommand,
Expand Down Expand Up @@ -51,4 +52,7 @@ export const commands: MilkdownPlugin[] = [

toggleLinkCommand,
updateLinkCommand,

isMarkSelectedCommand,
isNodeSelectedCommand,
]
1 change: 1 addition & 0 deletions packages/plugins/preset-commonmark/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[] = [
Expand Down