forked from appleple/smartblock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.tsx
172 lines (160 loc) · 4.9 KB
/
demo.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
import SmartBlock from './src/adapter';
import Code from './src/extensions/code';
import ImageDialogAdapter from './src/extensions/imageDialogAdapter';
import ImageWithText from './src/extensions/imageWithText';
import LinkDialogAdaper from './src/extensions/linkDialogAdapter';
import * as showdown from 'showdown';
import './css/smartblock.css';
import Paragraph from './src/extensions/paragraph';
import Trash from './src/extensions/trash';
import MoveUp from './src/extensions/move-up';
import MoveDown from './src/extensions/move-down';
import Heading from './src/extensions/heading';
import ListItem from './src/extensions/list-item';
import BulletList from './src/extensions/bullet-list';
import OrderedList from './src/extensions/ordered-list';
import Embed from './src/extensions/embed';
import Table from './src/extensions/table';
import Blockquote from './src/extensions/blockquote';
import Strong from './src/extensions/strong';
import Emphasis from './src/extensions/emphasis';
import Expert from './src/extensions/expert';
import Book from './src/extensions/book';
import Cta from './src/extensions/cta';
import Product from './src/extensions/product';
import HorizontalRule from './src/extensions/horizontal-rule';
import Sup from './src/extensions/sup';
import Underline from './src/extensions/underline';
import Strike from './src/extensions/strike';
import DefaultKeys from './src/extensions/default-keys';
import DefaultPlugins from './src/extensions/default-plugins';
import { Extension } from './src/types/';
import testContent from './testContent'
const getDialog = (attr, defaultAttrs?) => {
let onOKCl
let onCancelCl
let dialog = null
return (onOK, onCancel, attrs) => {
if (!dialog) {
const div = document.createElement('div')
div.style.position = 'fixed'
div.style.left = '50%'
div.style.top = '50%'
document.body.append(div)
const input = document.createElement('input')
input.style.width = '200px';
div.append(input)
const ok = document.createElement('button')
ok.innerHTML = 'OK'
div.append(ok)
const cancel = document.createElement('button')
cancel.innerHTML = 'Cancel'
div.append(cancel)
ok.addEventListener('click', () => {
onOKCl({ ...defaultAttrs, ...attrs, [attr]: input.value })
div.style.display = 'none'
})
cancel.addEventListener('click', () => {
onCancelCl()
div.style.display = 'none'
})
dialog = { div, input, ok, cancel }
}
onOKCl = onOK
onCancelCl = onCancel
dialog.input.value = attrs[attr]
dialog.div.style.display = 'block'
return dialog
}
}
const openLinkDialog = getDialog('href')
const openImageDialog = getDialog('caption')
const openImageTextDialog = getDialog('src')
const openExpertDialog = getDialog('name', { function: 'Haberkorn Expert für ' })
const openBookDialog = getDialog('title', { subtitle: 'Subtitle', id: 27 })
const openProductDialog = getDialog('title')
const openCtaDialog = getDialog('text', {
href: 'https://google.com', title: 'tooltip', variant: 'reverse'
})
const extensions = [
// blocks
new Paragraph(),
new Heading({ sizes: ['XL', 'L', 'M', 'S', 'XS'] }),
new ListItem(),
new BulletList(),
new OrderedList(),
new Embed(),
// new Code(),
new Table(),
new Blockquote(),
new HorizontalRule(),
// marks
new Strong(),
new Emphasis(),
new Underline(),
new Strike(),
// utility
new MoveDown(),
new MoveUp(),
new Trash(),
// default
new DefaultKeys(),
new DefaultPlugins({
placeholder: 'Content here...'
}),
new Code(),
new LinkDialogAdaper({
attributes: ['title', 'href', 'foo'],
openDialog: openLinkDialog,
}),
new ImageDialogAdapter({
attributes: ['title', 'src', 'caption'],
openDialog: openImageDialog,
aspectRatio: 16 / 9
}),
new ImageWithText({
attributes: ['title', 'src'],
openDialog: openImageTextDialog,
previewSrcFromAttrs: ({ src }) => src + '#XXXXXXXXX',
aspectRatio: 16 / 9
}),
new Expert({
attributes: ['name', 'userNo', 'function'],
openDialog: openExpertDialog
}),
new Book({
attributes: ['title', 'subtitle', 'id'],
openDialog: openBookDialog
}),
new Cta({
attributes: ['text', 'title', 'href', 'variant', 'class', 'foo'],
openDialog: openCtaDialog
}),
new Product({
attributes: ['id', 'title'],
openDialog: openProductDialog
}),
new Sup()
] as Extension[]
SmartBlock('#app', {
json: testContent,
showdown,
extensions,
onChange: ({ json }) => console.log('onChange', json)
});
// JSX version
// import * as React from 'react';
// import { render } from 'react-dom';
// import SmartBlock from './src/components/smartblock';
// import extensions from './src/extensions';
//
// render(<>
// <SmartBlock
// extensions={extensions}
// html="html"
// showTitle={true}
// onChange={({html}) => console.log(html)}
// />
// </>,
// document.getElementById('app')
// );