Skip to content

Commit e0714ef

Browse files
committed
poc: prosemirror tables
1 parent 40cc851 commit e0714ef

File tree

4 files changed

+92
-1
lines changed

4 files changed

+92
-1
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { Component, h, State } from '@stencil/core';
2+
/**
3+
* Text editor with tables (HTML mode only).
4+
*
5+
* When using the text editor in HTML mode, it is possible to paste and
6+
* display tables in the text editor.
7+
* Basic interaction with the table is supported, but you cannot do
8+
* complex operations
9+
*/
10+
@Component({
11+
tag: 'limel-example-text-editor-with-tables',
12+
shadow: true,
13+
})
14+
export class TextEditorWithTablesExample {
15+
@State()
16+
private value: string =
17+
'<table><tbody><tr><td style="background-color: rgb(25, 107, 36);color: white;"><p><strong>Column1</strong></p></td><td style="background-color: rgb(25, 107, 36);color: white;"><p><strong>Column2</strong></p></td></tr><tr><td style="background-color: rgb(193, 240, 200);color: black;"><p>Cell A1</p></td><td style="background-color: rgb(193, 240, 200);color: black;"><p>Cell B1</p></td></tr><tr><td style="color: black;"><p>Cell A2</p></td><td style="background-color: yellow;color: red;"><p>Cell B2</p></td></tr></tbody></table>';
18+
19+
@State()
20+
private readonly = false;
21+
22+
public render() {
23+
return [
24+
<limel-text-editor
25+
value={this.value}
26+
onChange={this.handleChange}
27+
readonly={this.readonly}
28+
contentType="html"
29+
/>,
30+
<limel-example-controls>
31+
<limel-checkbox
32+
checked={this.readonly}
33+
label="Readonly"
34+
onChange={this.setReadonly}
35+
/>
36+
</limel-example-controls>,
37+
<limel-example-value value={this.value} />,
38+
];
39+
}
40+
41+
private setReadonly = (event: CustomEvent<boolean>) => {
42+
event.stopPropagation();
43+
this.readonly = event.detail;
44+
};
45+
46+
private handleChange = (event: CustomEvent<string>) => {
47+
this.value = event.detail;
48+
};
49+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { tableNodes, tableEditing } from 'prosemirror-tables';
2+
3+
export const tableEditingPlugin = tableEditing;
4+
5+
export const getTableNodes = () => {
6+
return tableNodes({
7+
tableGroup: 'block',
8+
cellContent: 'block+',
9+
cellAttributes: {
10+
background: {
11+
default: null,
12+
getFromDOM: (dom) => {
13+
return dom.style.backgroundColor || null;
14+
},
15+
setDOMAttr: (value: string, attrs: Record<string, string>) => {
16+
if (value) {
17+
attrs.style =
18+
(attrs.style || '') + `background-color: ${value};`;
19+
}
20+
},
21+
},
22+
color: {
23+
default: null,
24+
getFromDOM: (dom) => {
25+
return dom.style.color || null;
26+
},
27+
setDOMAttr: (value: string, attrs: Record<string, string>) => {
28+
if (value) {
29+
attrs.style = (attrs.style || '') + `color: ${value};`;
30+
}
31+
},
32+
},
33+
},
34+
});
35+
};

src/components/text-editor/prosemirror-adapter/prosemirror-adapter.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import {
4040
import { createImageRemoverPlugin } from './plugins/image-remover-plugin';
4141
import { createMenuStateTrackingPlugin } from './plugins/menu-state-tracking-plugin';
4242
import { createActionBarInteractionPlugin } from './plugins/menu-action-interaction-plugin';
43+
import { getTableNodes, tableEditingPlugin } from './plugins/table-plugin';
4344

4445
const DEBOUNCE_TIMEOUT = 300;
4546

@@ -273,7 +274,11 @@ export class ProsemirrorAdapter {
273274

274275
private initializeSchema() {
275276
return new Schema({
276-
nodes: addListNodes(schema.spec.nodes, 'paragraph block*', 'block'),
277+
nodes: addListNodes(
278+
schema.spec.nodes,
279+
'paragraph block*',
280+
'block',
281+
).append(getTableNodes()),
277282
marks: schema.spec.marks.append({
278283
strikethrough: strikethrough,
279284
}),
@@ -310,6 +315,7 @@ export class ProsemirrorAdapter {
310315
this.updateActiveActionBarItems,
311316
),
312317
createActionBarInteractionPlugin(this.menuCommandFactory),
318+
tableEditingPlugin(),
313319
],
314320
});
315321
}

src/components/text-editor/text-editor.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { createRandomString } from '../../util/random-string';
1515
* @exampleComponent limel-example-text-editor-as-form-component
1616
* @exampleComponent limel-example-text-editor-with-markdown
1717
* @exampleComponent limel-example-text-editor-with-html
18+
* @exampleComponent limel-example-text-editor-with-tables
1819
* @exampleComponent limel-example-text-editor-allow-resize
1920
* @exampleComponent limel-example-text-editor-size
2021
* @exampleComponent limel-example-text-editor-ui

0 commit comments

Comments
 (0)