-
-
Notifications
You must be signed in to change notification settings - Fork 139
/
index.tsx
34 lines (31 loc) · 1.16 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import { realmPlugin } from '../../RealmWithPlugins'
import { Cell, useCellValues } from '@mdxeditor/gurx'
import React from 'react'
import { addTopAreaChild$, readOnly$ } from '../core'
import { Root } from './primitives/toolbar'
/**
* The factory function that returns the contents of the toolbar.
* @group Toolbar
*/
export const toolbarContents$ = Cell<() => React.ReactNode>(() => null)
const DEFAULT_TOOLBAR_CONTENTS = () => {
return 'This is an empty toolbar. Pass `{toolbarContents: () => { return <>toolbar components</> }}` to the toolbarPlugin to customize it.'
}
/**
* A plugin that adds a toolbar to the editor.
* @group Toolbar
*/
export const toolbarPlugin = realmPlugin<{ toolbarContents: () => React.ReactNode }>({
init(realm, params) {
realm.pubIn({
[toolbarContents$]: params?.toolbarContents ?? DEFAULT_TOOLBAR_CONTENTS,
[addTopAreaChild$]: () => {
const [toolbarContents, readOnly] = useCellValues(toolbarContents$, readOnly$)
return <Root readOnly={readOnly}>{toolbarContents()}</Root>
}
})
},
update(realm, params) {
realm.pub(toolbarContents$, params?.toolbarContents ?? DEFAULT_TOOLBAR_CONTENTS)
}
})