Skip to content

Commit 7f8a6ae

Browse files
committed
fix(text-editor tables): put repeated code in single function
1 parent 8a2f54b commit 7f8a6ae

File tree

3 files changed

+14
-40
lines changed

3 files changed

+14
-40
lines changed

src/components/text-editor/prosemirror-adapter/plugins/table-plugin.ts

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,34 +9,23 @@ export const getTableEditingPlugins = (tablesEnabled: boolean): Plugin[] => {
99
return [];
1010
};
1111

12+
const createStyleAttribute = (cssProperty: string) => ({
13+
default: null,
14+
getFromDOM: (dom: HTMLElement) => dom.style[cssProperty] || null,
15+
setDOMAttr: (value: string, attrs: Record<string, any>) => {
16+
if (value) {
17+
attrs.style = (attrs.style || '') + `${cssProperty}: ${value};`;
18+
}
19+
},
20+
});
21+
1222
export const getTableNodes = () => {
1323
return tableNodes({
1424
tableGroup: 'block',
1525
cellContent: 'block+',
1626
cellAttributes: {
17-
background: {
18-
default: null,
19-
getFromDOM: (dom) => {
20-
return dom.style.backgroundColor || null;
21-
},
22-
setDOMAttr: (value: string, attrs: Record<string, string>) => {
23-
if (value) {
24-
attrs.style =
25-
(attrs.style || '') + `background-color: ${value};`;
26-
}
27-
},
28-
},
29-
color: {
30-
default: null,
31-
getFromDOM: (dom) => {
32-
return dom.style.color || null;
33-
},
34-
setDOMAttr: (value: string, attrs: Record<string, string>) => {
35-
if (value) {
36-
attrs.style = (attrs.style || '') + `color: ${value};`;
37-
}
38-
},
39-
},
27+
background: createStyleAttribute('background-color'),
28+
color: createStyleAttribute('color'),
4029
},
4130
});
4231
};

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,6 @@ export class ProsemirrorAdapter {
101101
@Prop()
102102
triggerCharacters: TriggerCharacter[] = [];
103103

104-
@Prop()
105-
public supportTables: boolean = false;
106-
107104
@Element()
108105
private host: HTMLLimelTextEditorElement;
109106

@@ -308,7 +305,7 @@ export class ProsemirrorAdapter {
308305
});
309306
nodes = addListNodes(nodes, 'paragraph block*', 'block');
310307

311-
if (this.supportTables) {
308+
if (this.contentType === 'html') {
312309
nodes = nodes.append(getTableNodes());
313310
}
314311

@@ -351,7 +348,7 @@ export class ProsemirrorAdapter {
351348
this.updateActiveActionBarItems,
352349
),
353350
createActionBarInteractionPlugin(this.menuCommandFactory),
354-
...getTableEditingPlugins(this.supportTables),
351+
...getTableEditingPlugins(this.contentType === 'html'),
355352
],
356353
});
357354
}

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

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -155,13 +155,6 @@ export class TextEditor implements FormComponent<string> {
155155
@Prop({ reflect: true })
156156
public ui?: EditorUiType = 'standard';
157157

158-
/**
159-
* Set to `true` to allow parsing of table data. Only works when `type` is `html`.
160-
161-
*/
162-
@Prop({ reflect: true })
163-
public enableTables?: boolean;
164-
165158
/**
166159
* Dispatched when a change is made to the editor
167160
*/
@@ -255,17 +248,12 @@ export class TextEditor implements FormComponent<string> {
255248
aria-disabled={this.disabled}
256249
language={this.language}
257250
triggerCharacters={this.triggers}
258-
supportTables={this.checkForTables()}
259251
/>,
260252
this.renderPlaceholder(),
261253
this.renderHelperLine(),
262254
];
263255
}
264256

265-
private checkForTables() {
266-
return this.enableTables && this.contentType === 'html';
267-
}
268-
269257
private renderLabel() {
270258
if (!this.label) {
271259
return;

0 commit comments

Comments
 (0)