-
Notifications
You must be signed in to change notification settings - Fork 162
Expand file tree
/
Copy pathplugin.js
More file actions
261 lines (232 loc) · 6.49 KB
/
Copy pathplugin.js
File metadata and controls
261 lines (232 loc) · 6.49 KB
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
import Table from './table';
import * as $ from './utils/dom';
import { IconTable, IconTableWithHeadings, IconTableWithoutHeadings, IconStretch, IconCollapse } from '@codexteam/icons';
/**
* @typedef {object} TableData - configuration that the user can set for the table
* @property {number} rows - number of rows in the table
* @property {number} cols - number of columns in the table
*/
/**
* @typedef {object} Tune - setting for the table
* @property {string} name - tune name
* @property {HTMLElement} icon - icon for the tune
* @property {boolean} isActive - default state of the tune
* @property {void} setTune - set tune state to the table data
*/
/**
* @typedef {object} TableConfig - object with the data transferred to form a table
* @property {boolean} withHeading - setting to use cells of the first row as headings
* @property {string[][]} content - two-dimensional array which contains table content
*/
/**
* @typedef {object} TableConstructor
* @property {TableConfig} data — previously saved data
* @property {TableConfig} config - user config for Tool
* @property {object} api - Editor.js API
* @property {boolean} readOnly - read-only mode flag
*/
/**
* @typedef {import('@editorjs/editorjs').PasteEvent} PasteEvent
*/
/**
* Table block for Editor.js
*/
export default class TableBlock {
/**
* Notify core that read-only mode is supported
*
* @returns {boolean}
*/
static get isReadOnlySupported() {
return true;
}
/**
* Allow to press Enter inside the CodeTool textarea
*
* @returns {boolean}
* @public
*/
static get enableLineBreaks() {
return true;
}
/**
* Render plugin`s main Element and fill it with saved data
*
* @param {TableConstructor} init
*/
constructor({data, config, api, readOnly, block}) {
this.api = api;
this.readOnly = readOnly;
this.config = config;
this.data = {
withHeadings: this.getConfig('withHeadings', false, data),
stretched: this.getConfig('stretched', false, data),
content: data && data.content ? data.content : []
};
this.table = null;
this.block = block;
}
/**
* Get Tool toolbox settings
* icon - Tool icon's SVG
* title - title to show in toolbox
*
* @returns {{icon: string, title: string}}
*/
static get toolbox() {
return {
icon: IconTable,
title: 'Table'
};
}
/**
* Return Tool's view
*
* @returns {HTMLDivElement}
*/
render() {
/** creating table */
this.table = new Table(this.readOnly, this.api, this.data, this.config);
/** creating container around table */
this.container = $.make('div', this.api.styles.block);
this.container.appendChild(this.table.getWrapper());
this.table.setHeadingsSetting(this.data.withHeadings);
return this.container;
}
/**
* Hook called after the block is fully rendered and mounted
* Set this.block.stretched here as block is not initialized in render().
*
* @returns {void}
*/
rendered() {
if (this.block) {
this.block.stretched = !!this.data.stretched;
}
}
/**
* Returns plugin settings
*
* @returns {Array}
*/
renderSettings() {
return [
{
label: this.api.i18n.t('With headings'),
icon: IconTableWithHeadings,
isActive: this.data.withHeadings,
closeOnActivate: true,
toggle: true,
onActivate: () => {
this.data.withHeadings = true;
this._updateTunes();
}
}, {
label: this.api.i18n.t('Without headings'),
icon: IconTableWithoutHeadings,
isActive: !this.data.withHeadings,
closeOnActivate: true,
toggle: true,
onActivate: () => {
this.data.withHeadings = false;
this._updateTunes();
}
}, {
label: this.data.stretched ? this.api.i18n.t('Collapse') : this.api.i18n.t('Stretch'),
icon: this.data.stretched ? IconCollapse : IconStretch,
closeOnActivate: true,
toggle: true,
onActivate: () => {
this.data.stretched = !this.data.stretched;
this._updateTunes();
}
}
];
}
/**
* Handle updating UI for tunes
*
* @returns {void}
*/
_updateTunes() {
this.table.setHeadingsSetting(this.data.withHeadings);
if (this.block) {
this.block.stretched = !!this.data.stretched;
this.block.dispatchChange();
}
}
/**
* Extract table data from the view
*
* @returns {TableData} - saved data
*/
save() {
const tableContent = this.table.getData();
const result = {
withHeadings: this.data.withHeadings,
stretched: this.data.stretched,
content: tableContent
};
return result;
}
/**
* Plugin destroyer
*
* @returns {void}
*/
destroy() {
this.table.destroy();
}
/**
* A helper to get config value.
*
* @param {string} configName - the key to get from the config.
* @param {any} defaultValue - default value if config doesn't have passed key
* @param {object} savedData - previously saved data. If passed, the key will be got from there, otherwise from the config
* @returns {any} - config value.
*/
getConfig(configName, defaultValue = undefined, savedData = undefined) {
const data = this.data || savedData;
if (data) {
return data[configName] ? data[configName] : defaultValue;
}
return this.config && this.config[configName] ? this.config[configName] : defaultValue;
}
/**
* Table onPaste configuration
*
* @public
*/
static get pasteConfig() {
return { tags: ['TABLE', 'TR', 'TH', 'TD'] };
}
/**
* On paste callback that is fired from Editor
*
* @param {PasteEvent} event - event with pasted data
*/
onPaste(event) {
const table = event.detail.data;
/** Check if the first row is a header */
const firstRowHeading = table.querySelector(':scope > thead, tr:first-of-type th');
/** Get all rows from the table */
const rows = Array.from(table.querySelectorAll('tr'));
/** Generate a content matrix */
const content = rows.map((row) => {
/** Get cells from row */
const cells = Array.from(row.querySelectorAll('th, td'))
/** Return cells content */
return cells.map((cell) => cell.innerHTML);
});
/** Update Tool's data */
this.data = {
withHeadings: firstRowHeading !== null,
stretched: false,
content
};
/** Update table block */
if (this.table.wrapper) {
this.table.wrapper.replaceWith(this.render());
}
}
}