forked from textbus/textbus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.tsx
141 lines (128 loc) · 3.06 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
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
import 'reflect-metadata'
import { BrowserModule } from '@textbus/platform-browser'
import {
Commander,
ComponentInstance,
ContentType,
createVNode,
defineComponent,
FormatHostBindingRender,
Formatter,
onBreak,
Selection,
Slot,
Textbus,
useContext,
useSelf,
VElement,
VTextNode,
} from '@textbus/core'
import { createRoot } from 'react-dom/client'
import { Adapter, ViewComponentProps } from '@textbus/adapter-react'
const adapter = new Adapter({
RootComponent: App,
ParagraphComponent: Paragraph
}, (host, root) => {
const app = createRoot(host)
app.render(root)
return () => {
app.unmount()
}
})
const browserModule = new BrowserModule(document.getElementById('editor')!, {
adapter,
// useContentEditable: true
})
const textbus = new Textbus({
imports: [
browserModule
]
})
const fontSizeFormatter: Formatter<string> = {
name: 'fontSize',
render(children: Array<VElement | VTextNode | ComponentInstance>, formatValue: string): VElement | FormatHostBindingRender {
return createVNode('span', {
style: {
fontSize: formatValue
}
}, children)
}
}
const rootComponent = defineComponent({
name: 'RootComponent',
type: ContentType.BlockComponent,
validate() {
return {
slots: [
new Slot([
ContentType.Text,
ContentType.BlockComponent
])
]
}
},
setup() {
const slot = useSelf().slots.get(0)!
for (let i = 0; i < 50; i++) {
const p = paragraphComponent.createInstance(useContext())
p.slots.first.insert(Math.random().toString(16), fontSizeFormatter, '23px')
slot.insert(p)
}
}
})
const paragraphComponent = defineComponent({
name: 'ParagraphComponent',
type: ContentType.BlockComponent,
validate(initData) {
return {
slots: initData?.slots || [new Slot([
ContentType.Text
])]
}
},
setup() {
const context = useContext()
const commander = useContext(Commander)
const selection = useContext(Selection)
const self = useSelf()
onBreak(ev => {
ev.preventDefault()
const nextContent = ev.target.cut(ev.data.index)
const p = paragraphComponent.createInstance(context, {
slots: [nextContent]
})
commander.insertAfter(p, self)
selection.selectFirstPosition(p)
})
return {
show() {
return 'dfsafdas'
}
}
}
})
const rootModel = rootComponent.createInstance(textbus)
textbus.render(rootModel)
function App(props: ViewComponentProps<typeof rootComponent>) {
const slot = props.component.slots.first
return (
<div className="xxxx">
{
adapter.slotRender(slot, children => {
return createVNode('div', {
'textbus-document': 'true',
ref: props.rootRef
}, children)
})
}
</div>
)
}
function Paragraph(props: ViewComponentProps<typeof paragraphComponent>) {
const slot = props.component.slots.first
return (
adapter.slotRender(slot, children => {
return createVNode('p', { ref: props.rootRef }, children)
})
)
}