forked from ProseMirror/prosemirror-tables
-
Notifications
You must be signed in to change notification settings - Fork 24
/
demo.js
91 lines (85 loc) · 2.65 KB
/
demo.js
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
import {EditorView} from 'prosemirror-view';
import {EditorState} from 'prosemirror-state';
import {DOMParser, Schema} from 'prosemirror-model';
import {schema as baseSchema} from 'prosemirror-schema-basic';
import {keymap} from 'prosemirror-keymap';
import {exampleSetup, buildMenuItems} from 'prosemirror-example-setup';
import {MenuItem, Dropdown} from 'prosemirror-menu';
import {
addColumnAfter,
addColumnBefore,
deleteColumn,
addRowAfter,
addRowBefore,
deleteRow,
mergeCells,
splitCell,
setCellAttr,
toggleHeaderRow,
toggleHeaderColumn,
toggleHeaderCell,
goToNextCell,
deleteTable
} from './src/commands';
import {tableEditing, columnResizing, tableNodes, fixTables} from './src';
const schema = new Schema({
nodes: baseSchema.spec.nodes.append(
tableNodes({
tableGroup: 'block',
cellContent: 'block+',
cellAttributes: {
background: {
default: null,
getFromDOM(dom) {
return dom.style.backgroundColor || null;
},
setDOMAttr(value, attrs) {
if (value)
attrs.style = (attrs.style || '') + `background-color: ${value};`;
}
}
}
})
),
marks: baseSchema.spec.marks
});
const menu = buildMenuItems(schema).fullMenu;
function item(label, cmd) {
return new MenuItem({label, select: cmd, run: cmd});
}
const tableMenu = [
item('Insert column before', addColumnBefore),
item('Insert column after', addColumnAfter),
item('Delete column', deleteColumn),
item('Insert row before', addRowBefore),
item('Insert row after', addRowAfter),
item('Delete row', deleteRow),
item('Delete table', deleteTable),
item('Merge cells', mergeCells),
item('Split cell', splitCell),
item('Toggle header column', toggleHeaderColumn),
item('Toggle header row', toggleHeaderRow),
item('Toggle header cells', toggleHeaderCell),
item('Make cell green', setCellAttr('background', '#dfd')),
item('Make cell not-green', setCellAttr('background', null))
];
menu.splice(2, 0, [new Dropdown(tableMenu, {label: 'Table'})]);
const doc = DOMParser.fromSchema(schema).parse(
document.querySelector('#content')
);
let state = EditorState.create({
doc,
plugins: [
columnResizing(),
tableEditing(),
keymap({
Tab: goToNextCell(1),
'Shift-Tab': goToNextCell(-1)
})
].concat(exampleSetup({schema, menuContent: menu}))
});
const fix = fixTables(state);
if (fix) state = state.apply(fix.setMeta('addToHistory', false));
window.view = new EditorView(document.querySelector('#editor'), {state});
document.execCommand('enableObjectResizing', false, false);
document.execCommand('enableInlineTableEditing', false, false);