-
-
Notifications
You must be signed in to change notification settings - Fork 170
/
Copy pathhtml.tsx
206 lines (189 loc) · 5.95 KB
/
html.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import React from 'react'
import {
DiffSourceToggleWrapper,
MDXEditor,
diffSourcePlugin,
headingsPlugin,
toolbarPlugin,
$createGenericHTMLNode,
activeEditor$,
currentSelection$
} from '../'
import { $patchStyleText } from '@lexical/selection'
import { $getRoot, $getSelection, $isRangeSelection, $isTextNode, ElementNode, LexicalNode } from 'lexical'
import { $isGenericHTMLNode } from '../plugins/core/GenericHTMLNode'
import { GenericHTMLNode } from '../plugins/core/GenericHTMLNode'
import { MdxJsxAttribute } from 'mdast-util-mdx'
import { $getNearestNodeOfType } from '@lexical/utils'
import { useCellValues } from '@mdxeditor/gurx'
const markdownWithSpan = `
# Hello World
foo
<!-- comment -->
bar
A paragraph with <span style="color: red" class="some">some red text <span style="color: blue">with some blue nesting.</span> in here.</span> in it.
`
export function HTag() {
return (
<>
<MDXEditor
markdown={`
Hello world
<h1>hello world</h1>
`}
plugins={[headingsPlugin(), diffSourcePlugin()]}
onChange={(md) => {
console.log('change', md)
}}
/>
</>
)
}
export function SpanWithColor() {
return (
<>
<MDXEditor
markdown={markdownWithSpan}
plugins={[
headingsPlugin(),
diffSourcePlugin(),
toolbarPlugin({
toolbarContents: () => (
<DiffSourceToggleWrapper>
<HTMLToolbarComponent />
</DiffSourceToggleWrapper>
)
})
]}
onChange={(md) => {
console.log('change', md)
}}
/>
</>
)
}
const HTMLToolbarComponent = () => {
const [currentSelection, activeEditor] = useCellValues(currentSelection$, activeEditor$)
const currentStyle = React.useMemo(() => {
return (
activeEditor?.getEditorState().read(() => {
const selectedNodes = currentSelection?.getNodes() ?? []
if (selectedNodes.length === 1) {
let node: ElementNode | LexicalNode | null | undefined = selectedNodes[0]
let style = ''
while (!style && node && node !== $getRoot()) {
if ($isTextNode(node) || $isGenericHTMLNode(node)) {
style = node.getStyle()
}
node = node.getParent()
}
return style
} else {
return ''
}
}) ?? ''
)
}, [currentSelection, activeEditor])
const currentHTMLNode = React.useMemo(() => {
return (
activeEditor?.getEditorState().read(() => {
const selectedNodes = currentSelection?.getNodes() ?? []
if (selectedNodes.length === 1) {
return $getNearestNodeOfType(selectedNodes[0], GenericHTMLNode)
} else {
return null
}
}) ?? null
)
}, [currentSelection, activeEditor])
return (
<>
<button
onClick={() => {
if (activeEditor !== null && currentSelection !== null) {
activeEditor.update(() => {
$patchStyleText(currentSelection, { color: 'orange' })
})
}
}}
>
Make selection orange
</button>
<button
onClick={() => {
if (activeEditor !== null && currentSelection !== null) {
activeEditor.update(() => {
$patchStyleText(currentSelection, { 'font-size': '20px' })
})
}
}}
>
Big font size
</button>
{currentStyle && <div>Current style: {currentStyle}</div>}
current css class:{' '}
<input
disabled={currentHTMLNode === null}
value={getCssClass(currentHTMLNode)}
onChange={(e) => {
activeEditor?.update(
() => {
const attributesWithoutClass = currentHTMLNode?.getAttributes().filter((attr) => attr.name !== 'class') ?? []
const newClassAttr: MdxJsxAttribute = { type: 'mdxJsxAttribute', name: 'class', value: e.target.value }
currentHTMLNode?.updateAttributes([...attributesWithoutClass, newClassAttr])
},
{ discrete: true }
)
e.target.focus()
}}
/>
<button
disabled={currentHTMLNode === null}
onClick={() => {
if (activeEditor !== null && currentSelection !== null) {
activeEditor.update(() => {
// const children = currentHTMLNode?.getChildren() || []
currentHTMLNode?.remove()
const selection = $getSelection()
selection?.insertNodes(currentHTMLNode?.getChildren() ?? [])
})
}
}}
>
remove HTML node
</button>
<button
onClick={() => {
if (activeEditor !== null && currentSelection !== null) {
activeEditor.update(() => {
const selection = $getSelection()
if ($isRangeSelection(selection)) {
const selectedNodes = selection.getNodes()
const currentTextNode = selectedNodes.length === 1 && $isTextNode(selectedNodes[0]) ? selectedNodes[0] : null
if (currentTextNode) {
const attributes = Object.entries({ style: 'color: green' }).map(([name, value]) => ({
type: 'mdxJsxAttribute' as const,
name,
value
}))
const newNode = $createGenericHTMLNode('span', 'mdxJsxTextElement', attributes)
selection.insertNodes([newNode])
// newNode.insertAfter(slicedPortion)
// newNode.append(slicedPortion)
/*
$wrapNodeInElement(slicedPortion, () => )
*/
}
}
})
}
}}
>
wrap in a red span
</button>
</>
)
}
function getCssClass(node: GenericHTMLNode | null) {
return (node?.getAttributes().find((attr) => attr.name === 'class')?.value as string | undefined) ?? ''
}