MCPcopy Create free account
hub / github.com/editablejs/editable / withMark

Function withMark

packages/plugins/mark/src/plugin/with-mark.tsx:49–133  ·  view source on GitHub ↗
(editor: T, options: MarkOptions = {})

Source from the content-addressed store, hash-verified

47])
48
49export const withMark = <T extends Editable>(editor: T, options: MarkOptions = {}) => {
50 const newEditor = editor as T & MarkEditor
51
52 setOptions(newEditor, options)
53
54 const { renderLeaf } = newEditor
55
56 newEditor.toggleMark = (format: MarkFormat) => {
57 if (!MarkEditor.isEnabled(editor, format)) return
58 const active = MarkEditor.isActive(editor, format)
59
60 newEditor.normalizeSelection(selection => {
61 if (newEditor.selection !== selection) newEditor.selection = selection
62
63 if (active) {
64 Editor.removeMark(newEditor, format)
65 } else {
66 if (format === 'sub') {
67 Editor.removeMark(newEditor, 'sup')
68 } else if (format === 'sup') {
69 Editor.removeMark(newEditor, 'sub')
70 }
71 Editor.addMark(newEditor, format, true)
72 }
73 })
74 }
75
76 newEditor.renderLeaf = ({ attributes, children, text }: RenderLeafProps<Mark>) => {
77 const style: React.CSSProperties = attributes.style ?? {}
78 if (text.bold && MarkEditor.isEnabled(editor, 'bold')) {
79 style.fontWeight = typeof text.bold === 'string' ? text.bold : 'bold'
80 } else {
81 style.fontWeight = undefined
82 }
83
84 if (text.italic && MarkEditor.isEnabled(editor, 'italic')) {
85 style.fontStyle = 'italic'
86 }
87
88 if (text.underline && MarkEditor.isEnabled(editor, 'underline')) {
89 style.textDecoration = 'underline'
90 }
91
92 if (text.strikethrough && MarkEditor.isEnabled(editor, 'strikethrough')) {
93 style.textDecoration = style.textDecoration
94 ? style.textDecoration + ' line-through'
95 : 'line-through'
96 }
97
98 const enabledSub = text.sub && MarkEditor.isEnabled(editor, 'sub')
99 if (enabledSub) {
100 children = <SubStyles>{children}</SubStyles>
101 }
102 const enabledSup = text.sup && MarkEditor.isEnabled(editor, 'sup')
103 if (enabledSup) {
104 children = <SupStyles>{children}</SupStyles>
105 }
106

Callers 1

withPluginsFunction · 0.85

Calls 2

setOptionsFunction · 0.90
withShortcutsFunction · 0.90

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…