-
Notifications
You must be signed in to change notification settings - Fork 158
Expand file tree
/
Copy pathplugin.js
More file actions
309 lines (281 loc) · 7.97 KB
/
plugin.js
File metadata and controls
309 lines (281 loc) · 7.97 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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
import Table from './table';
import tableIcon from './img/tableIcon.svg';
import withHeadings from './img/with-headings.svg';
import withoutHeadings from './img/without-headings.svg';
import * as $ from './utils/dom';
/**
* @typedef {object} TableConfig - 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} TableData - 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
*/
/**
* 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 {TableData} data — previously saved data
* @param {TableConfig} config - user config for Tool
* @param {object} api - Editor.js API
* @param {boolean} readOnly - read-only mode flag
*/
constructor({ data, config, api, readOnly }) {
this.api = api;
this.readOnly = readOnly;
this.data = {
withHeadings: data && data.withHeadings ? data.withHeadings : false,
content: data && data.content ? data.content : []
};
this.config = config;
this.table = null;
}
/**
* 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: tableIcon,
title: 'Table'
};
}
/**
* Plugins styles
*
* @returns {{settingsWrapper: string}}
*/
static get CSS() {
return {
settingsWrapper: 'tc-settings'
};
}
/**
* 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;
}
/**
* Add plugin settings
*
* @returns {HTMLElement} - wrapper element
*/
renderSettings() {
const wrapper = $.make('div', TableBlock.CSS.settingsWrapper);
const tunes = [ {
name: this.api.i18n.t('With headings'),
icon: withHeadings,
isActive: this.data.withHeadings,
setTune: () => {
this.data.withHeadings = true;
}
}, {
name: this.api.i18n.t('Without headings'),
icon: withoutHeadings,
isActive: !this.data.withHeadings,
setTune: () => {
this.data.withHeadings = false;
}
} ];
tunes.forEach((tune) => {
let tuneButton = $.make('div', this.api.styles.settingsButton);
if (tune.isActive) {
tuneButton.classList.add(this.api.styles.settingsButtonActive);
}
tuneButton.innerHTML = tune.icon;
tuneButton.addEventListener('click', () => this.toggleTune(tune, tuneButton));
this.api.tooltip.onHover(tuneButton, tune.name, {
placement: 'top',
hidingDelay: 500
});
wrapper.append(tuneButton);
});
return wrapper;
}
/**
* Table Tool on paste configuration
*
* @public
*/
static get pasteConfig() {
return {
tags: ["table", "tr", "td", "th"]
};
}
/**
* On paste callback that is fired from Editor
* ready for word table or html table
*
* @param {PasteEvent} event - event with pasted data
*/
onPaste(event) {
if (event.tag = "table") {
//get table body
let tbody = event.detail.data.outerHTML;
//get trs
const trregexp = /<tr[^>]*?>(.*?)<\/tr>/gism;
let tr = [...tbody.matchAll(trregexp)];
//if first item is th,set withHeadings true
if (tr.length > 0 && /<th[^>]*?>.*?<\/th>/gism.test(tr[0]))
this.data.withHeadings = true;
//ups is rowspan place holder
let ups = [];
//each tr line
for (let i = 0; i < tr.length; i++) {
let trmatch = tr[i];
let subdata = [];
//get tds
const tdregexp = /<(t[hd])([^>]*?)>(.*?)<\/\1>/gism;
let td = [...trmatch[1].matchAll(tdregexp)];
for (let j = 0; j < td.length; j++) {
let tdmatch = td[j];
//get colspan,rowspan of td
let colspan = 0;
let rowspan = 0;
let colmatch = tdmatch[2].match(/colspan[^\d]*(\d+)/i);
if (colmatch)
colspan = parseInt(colmatch[1]);
let rowmatch = tdmatch[2].match(/rowspan[^\d]*(\d+)/i);
if (rowmatch)
rowspan = parseInt(rowmatch[1]);
subdata.push((tdmatch[3]).trim());
//cursor of col
let offset = 0;
ups.forEach(function (up) {
if (up[0] == i && up[1] <= j) {
offset++;
}
});
//mark rowspan place holder
while (rowspan > 1) {
ups.push([i + rowspan - 1, j + offset]);
let tmpcol = colspan;
while (tmpcol > 1) {
ups.push([i + rowspan - 1, j + offset + tmpcol - 1]);
tmpcol--;
}
rowspan--;
}
//push "<" to colspan place
while (colspan > 1) {
subdata.push("<");
colspan--;
}
}
//push "^" to each rowspan place
ups.forEach(function (up) {
if (up[0] == i) {
subdata.splice(up[1], 0, "^");
}
});
this.data.content.push(subdata);
}
//some data need standardization, such as "half select tabe" from html
//get max col of content data
let maxcol = 0;
this.data.content.forEach(function (row) {
if (row.length > maxcol)
maxcol = row.length;
});
//add missing data,first line to left,other line to right
for(let k=0;k<this.data.content.length;k++){
while(this.data.content[k].length<maxcol){
if(k==0)
{
this.data.content[k].splice(0,0," ");
}
else
{
this.data.content[k].push("");
}
}
}
//render table
const oldView = this.table.wrapper;
if (oldView) {
oldView.parentNode.replaceChild(this.render(), oldView);
}
}
}
/**
* Extract table data from the view
*
* @returns {TableData} - saved data
*/
save() {
const tableContent = this.table.getData();
let result = {
withHeadings: this.data.withHeadings,
content: tableContent
};
return result;
}
/**
* Changes the state of the tune
* Updates its representation in the table
*
* @param {Tune} tune - one of the table settings
* @param {HTMLElement} tuneButton - DOM element of the tune
* @returns {void}
*/
toggleTune(tune, tuneButton) {
const buttons = tuneButton.parentNode.querySelectorAll('.' + this.api.styles.settingsButton);
// Clear other buttons
Array.from(buttons).forEach((button) =>
button.classList.remove(this.api.styles.settingsButtonActive)
);
// Mark active button
tuneButton.classList.toggle(this.api.styles.settingsButtonActive);
tune.setTune();
this.table.setHeadingsSetting(this.data.withHeadings);
}
/**
* Plugin destroyer
*
* @returns {void}
*/
destroy() {
this.table.destroy();
}
}