Repository navigation
Expand file tree
/
Copy pathplugin.js
More file actions
117 lines (105 loc) · 2.55 KB
/
Copy pathplugin.js
File metadata and controls
117 lines (105 loc) · 2.55 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
const TableConstructor = require("./tableConstructor").TableConstructor;
const svgIcon = require("./img/toolboxIcon.svg");
const CSS = {
input: "tc-table__inp"
};
/**
* Tool for table's creating
* @typedef {object} TableData - object with the data transferred to form a table
* @property {string[][]} content - two-dimensional array which contains table content
*/
class Table {
/**
* Allow to press Enter inside the CodeTool textarea
* @returns {boolean}
* @public
*/
static get enableLineBreaks() {
return true;
}
/**
* Get Tool toolbox settings
* icon - Tool icon's SVG
* title - title to show in toolbox
*
* @return {{icon: string, title: string}}
*/
static get toolbox() {
return {
icon: svgIcon,
title: "Table"
};
}
/**
* Render plugin`s main Element and fill it with saved data
* @param {TableData} data — previously saved data
* @param {object} config - user config for Tool
* @param {object} api - Editor.js API
*/
constructor({ data, config, api }) {
if (data.hasOwnProperty("content")) {
let arr = [];
for (let index in data.content) {
arr.push(data.content[index]);
}
data.content = arr;
}
console.log(data);
this.api = api;
this._tableConstructor = new TableConstructor(data, config, api);
}
/**
* Return Tool's view
* @returns {HTMLDivElement}
* @public
*/
render() {
return this._tableConstructor.htmlElement;
}
/**
* Firestore compatible
* @returns {{<Array>}} - saved data
* @public
*/
formatNestedArray(arr) {
let obj = {};
for (let i = 0; i < arr.length; i++) {
obj[i] = arr[i];
}
return obj;
}
/**
* Extract Tool's data from the view
* @returns {TableData} - saved data
* @public
*/
save(toolsContent) {
const table = toolsContent.querySelector("table");
const data = [];
const rows = table.rows;
for (let i = 0; i < rows.length; i++) {
const row = rows[i];
const cols = Array.from(row.cells);
const inputs = cols.map(cell => cell.querySelector("." + CSS.input));
const isWorthless = inputs.every(this._isEmpty);
if (isWorthless) {
continue;
}
data.push(inputs.map(input => input.innerHTML));
}
return {
content: this.formatNestedArray(data)
};
}
/**
* @private
*
* Check input field is empty
* @param {HTMLElement} input - input field
* @return {boolean}
*/
_isEmpty(input) {
return !input.textContent.trim();
}
}
module.exports = Table;