-
Notifications
You must be signed in to change notification settings - Fork 16
Add tables plugin to text editor #3301
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
acbc3d8
chore(package): add prosemirror-tables
DavidAkkerman 1a28d75
feat(text-editor tables): add basic plugin for parsing tables
DavidAkkerman 031affb
feat(text-editor tables): set enableTables true if contentType is html
john-traas 29cabc1
fix(text-editor tables): put repeated code in single function
john-traas 429db9d
feat(text-editor tables): fix example component
john-traas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
src/components/text-editor/examples/text-editor-with-tables.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| import { Component, h, State } from '@stencil/core'; | ||
| /** | ||
| * Text editor with tables (HTML mode only). | ||
| * | ||
| * Basic table support is available when using the text editor in `HTML` mode. | ||
| * This allows you to paste and display tables in the text editor. | ||
| * Complex operations are not supported, adding and removing columns are not supported. | ||
| * | ||
| * Tables will only appear as expected in text-editor fields that are in `HTML` mode. | ||
| */ | ||
| @Component({ | ||
| tag: 'limel-example-text-editor-with-tables', | ||
| shadow: true, | ||
| }) | ||
| export class TextEditorWithTablesExample { | ||
| @State() | ||
| private value: string = `<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="background-color: yellow;color: red;"><p>Cell A2</p></td> | ||
| <td style="background-color: yellow;color: red;"><p>Cell B2</p></td> | ||
| </tr> | ||
| </tbody></table>`; | ||
|
|
||
| @State() | ||
| private readonly = false; | ||
|
|
||
| public render() { | ||
| return [ | ||
| <limel-text-editor | ||
| key="html-editor" | ||
| value={this.value} | ||
| onChange={this.handleChange} | ||
| readonly={this.readonly} | ||
| contentType="html" | ||
| />, | ||
| <limel-example-controls key="controls"> | ||
| <limel-checkbox | ||
| checked={this.readonly} | ||
| label="Readonly" | ||
| onChange={this.setReadonly} | ||
| /> | ||
| </limel-example-controls>, | ||
| <limel-example-value key="example-value" value={this.value} />, | ||
| ]; | ||
| } | ||
|
|
||
| private setReadonly = (event: CustomEvent<boolean>) => { | ||
| event.stopPropagation(); | ||
| this.readonly = event.detail; | ||
| }; | ||
|
|
||
| private handleChange = (event: CustomEvent<string>) => { | ||
| this.value = event.detail; | ||
| }; | ||
| } |
31 changes: 31 additions & 0 deletions
31
src/components/text-editor/prosemirror-adapter/plugins/table-plugin.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| import { tableNodes, tableEditing } from 'prosemirror-tables'; | ||
| import { Plugin } from 'prosemirror-state'; | ||
|
|
||
| export const getTableEditingPlugins = (tablesEnabled: boolean): Plugin[] => { | ||
| if (tablesEnabled) { | ||
| return [tableEditing()]; | ||
| } | ||
|
|
||
| return []; | ||
| }; | ||
|
|
||
| const createStyleAttribute = (cssProperty: string) => ({ | ||
| default: null, | ||
| getFromDOM: (dom: HTMLElement) => dom.style[cssProperty] || null, | ||
| setDOMAttr: (value: string, attrs: Record<string, any>) => { | ||
| if (value) { | ||
| attrs.style = (attrs.style || '') + `${cssProperty}: ${value};`; | ||
| } | ||
| }, | ||
| }); | ||
|
|
||
| export const getTableNodes = () => { | ||
| return tableNodes({ | ||
| tableGroup: 'block', | ||
| cellContent: 'block+', | ||
| cellAttributes: { | ||
| background: createStyleAttribute('background-color'), | ||
| color: createStyleAttribute('color'), | ||
| }, | ||
| }); | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.