Skip to content

Commit d6bdb2d

Browse files
authored
feat: add math in slash menu (#1686)
1 parent 6b0192c commit d6bdb2d

File tree

5 files changed

+37
-4
lines changed

5 files changed

+37
-4
lines changed

Diff for: packages/crepe/src/feature/block-edit/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ interface BlockEditConfig {
4444
slashMenuCodeBlockLabel: string
4545
slashMenuTableIcon: Icon
4646
slashMenuTableLabel: string
47+
slashMenuMathIcon: Icon
48+
slashMenuMathLabel: string
4749
}
4850

4951
export type BlockEditFeatureConfig = Partial<BlockEditConfig>

Diff for: packages/crepe/src/feature/block-edit/menu/component.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ export const menuComponent: Component<MenuProps> = ({
2828
filter,
2929
config,
3030
}) => {
31-
const { groups, size } = useMemo(() => getGroups(filter, config), [filter])
31+
const { groups, size } = useMemo(
32+
() => getGroups(filter, config, ctx),
33+
[filter]
34+
)
3235

3336
const host = useHost()
3437
const [hoverIndex, setHoverIndex] = useState(0)

Diff for: packages/crepe/src/feature/block-edit/menu/config.ts

+29-2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import {
2828
tableIcon,
2929
textIcon,
3030
todoListIcon,
31+
functionsIcon,
3132
} from '../../../icons'
3233
import type { BlockEditFeatureConfig } from '../index'
3334
import type { MenuItemGroup } from './utils'
@@ -38,8 +39,18 @@ import {
3839
clearRange,
3940
} from './utils'
4041
import { GroupBuilder } from './group-builder'
42+
import type { Ctx } from '@milkdown/kit/ctx'
43+
import { FeaturesCtx } from '../../../core/slice'
44+
import { CrepeFeature } from '../../..'
45+
46+
export function getGroups(
47+
filter?: string,
48+
config?: BlockEditFeatureConfig,
49+
ctx?: Ctx
50+
) {
51+
const flags = ctx?.get(FeaturesCtx)
52+
const isLatexEnabled = flags?.includes(CrepeFeature.Latex)
4153

42-
export function getGroups(filter?: string, config?: BlockEditFeatureConfig) {
4354
const groupBuilder = new GroupBuilder()
4455
groupBuilder
4556
.addGroup('text', config?.slashMenuTextGroupLabel ?? 'Text')
@@ -200,7 +211,7 @@ export function getGroups(filter?: string, config?: BlockEditFeatureConfig) {
200211
},
201212
})
202213

203-
groupBuilder
214+
const advancedGroup = groupBuilder
204215
.addGroup('advanced', config?.slashMenuAdvancedGroupLabel ?? 'Advanced')
205216
.addItem('image', {
206217
label: config?.slashMenuImageLabel ?? 'Image',
@@ -250,6 +261,22 @@ export function getGroups(filter?: string, config?: BlockEditFeatureConfig) {
250261
},
251262
})
252263

264+
if (isLatexEnabled) {
265+
advancedGroup.addItem('math', {
266+
label: config?.slashMenuMathLabel ?? 'Math',
267+
icon: config?.slashMenuMathIcon?.() ?? functionsIcon,
268+
onRun: (ctx) => {
269+
const view = ctx.get(editorViewCtx)
270+
const { dispatch, state } = view
271+
272+
const command = clearContentAndAddBlockType(codeBlockSchema.type(ctx), {
273+
language: 'LaTex',
274+
})
275+
command(state, dispatch)
276+
},
277+
})
278+
}
279+
253280
config?.buildMenu?.(groupBuilder)
254281

255282
let groups = groupBuilder.build()

Diff for: packages/crepe/src/feature/toolbar/component.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ import {
2525
italicIcon,
2626
linkIcon,
2727
strikethroughIcon,
28+
functionsIcon,
2829
} from '../../icons'
2930
import type { ToolbarFeatureConfig } from './index'
30-
import { functionsIcon } from '../../icons/functions'
3131
import { NodeSelection, TextSelection } from '@milkdown/kit/prose/state'
3232
import { mathInlineSchema } from '../latex/inline-latex'
3333
import { FeaturesCtx } from '../../core/slice'

Diff for: packages/crepe/src/icons/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,4 @@ export * from './strikethrough'
3434
export * from './table'
3535
export * from './text'
3636
export * from './todo-list'
37+
export * from './functions'

0 commit comments

Comments
 (0)