-
Notifications
You must be signed in to change notification settings - Fork 8
feat(editor): add rich text editor toolbar #3173
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
Draft
jpzwarte
wants to merge
8
commits into
main
Choose a base branch
from
claude/add-editor-toolbar-OQAwK
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b4d4326
feat(editor): add rich text editor toolbar
claude b17f7d2
feat(editor): make bold/italic/underline/strikethrough buttons icon-o…
claude cbbe48b
🐫
jpzwarte cabf0a4
☀️
jpzwarte 617163e
🐳
jpzwarte 55fc157
🍰
jpzwarte b9f1328
🍊
jpzwarte 4c9b235
💙
jpzwarte 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
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,19 @@ | ||
| --- | ||
| '@sl-design-system/editor': minor | ||
| --- | ||
|
|
||
| Add rich text editor toolbar with common formatting actions. | ||
|
|
||
| A new `<sl-editor-toolbar>` component is now rendered inside `<sl-editor>` by default. It provides buttons for: | ||
|
|
||
| - **Text marks**: Bold, Italic, Underline, Strikethrough, Inline code | ||
| - **Block formats**: Blockquote, Heading 1–3 | ||
| - **Lists**: Bullet list, Ordered list | ||
| - **History**: Undo, Redo | ||
|
|
||
| The toolbar uses `<sl-tool-bar>` and `<sl-button>` from the design system, supports overflow and keyboard navigation, and reflects the current editor selection state via `aria-pressed`. | ||
|
|
||
| ### New API | ||
|
|
||
| - `Editor.toolbar` (`boolean`, default `true`) — set to `false` to hide the toolbar | ||
| - `EditorToolbar` — exported class for the toolbar element (`sl-editor-toolbar`) |
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 |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| export * from './src/editor.js'; | ||
| export * from './src/editor-toolbar.js'; |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,5 @@ | ||
| import { Editor } from './src/editor.js'; | ||
| import { EditorToolbar } from './src/editor-toolbar.js'; | ||
|
|
||
| customElements.define('sl-editor', Editor); | ||
| customElements.define('sl-editor-toolbar', EditorToolbar); |
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,9 @@ | ||
| :host { | ||
| display: block; | ||
| } | ||
|
|
||
| sl-tool-bar { | ||
| border-block-end: var(--sl-size-borderWidth-default) solid var(--sl-color-border-plain); | ||
| padding-block: var(--sl-size-050); | ||
| padding-inline: var(--sl-size-050); | ||
| } |
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,235 @@ | ||
| import { fixture } from '@sl-design-system/vitest-browser-lit'; | ||
| import { html } from 'lit'; | ||
| import { TextSelection } from 'prosemirror-state'; | ||
| import { afterEach, beforeEach, describe, expect, it } from 'vitest'; | ||
| import '../register.js'; | ||
| import { EditorToolbar } from './editor-toolbar.js'; | ||
| import { Editor } from './editor.js'; | ||
|
|
||
| describe('sl-editor-toolbar', () => { | ||
| let editor: Editor; | ||
| let toolbar: EditorToolbar; | ||
|
|
||
| const getButtonByIconName = (iconName: string): HTMLElement | null => { | ||
| return toolbar.renderRoot.querySelector<HTMLElement>(`sl-icon[name="${iconName}"]`)?.closest('sl-button') ?? null; | ||
| }; | ||
|
|
||
| const getNativeButtonByIconName = (iconName: string): HTMLButtonElement | null => { | ||
| return getButtonByIconName(iconName)?.shadowRoot?.querySelector('button') ?? null; | ||
| }; | ||
|
|
||
| describe('defaults', () => { | ||
| beforeEach(async () => { | ||
| editor = await fixture<Editor>(html`<sl-editor></sl-editor>`); | ||
| toolbar = editor.renderRoot.querySelector('sl-editor-toolbar')!; | ||
|
|
||
| // Wait for toolbar to update with view | ||
| await toolbar.updateComplete; | ||
| }); | ||
|
|
||
| it('should render a toolbar inside the editor', () => { | ||
| expect(toolbar).to.exist; | ||
| }); | ||
|
|
||
| it('should have an sl-tool-bar element', () => { | ||
| const toolBar = toolbar.renderRoot.querySelector('sl-tool-bar'); | ||
| expect(toolBar).to.exist; | ||
| }); | ||
|
|
||
| it('should render a Bold button', () => { | ||
| const button = getButtonByIconName('editor-bold'); | ||
| expect(button).to.exist; | ||
| }); | ||
|
|
||
| it('should render an Italic button', () => { | ||
| const button = getButtonByIconName('editor-italic'); | ||
| expect(button).to.exist; | ||
| }); | ||
|
|
||
| it('should render an Underline button', () => { | ||
| const button = getButtonByIconName('editor-underline'); | ||
| expect(button).to.exist; | ||
| }); | ||
|
|
||
| it('should render a Strikethrough button', () => { | ||
| const button = getButtonByIconName('editor-strikethrough'); | ||
| expect(button).to.exist; | ||
| }); | ||
|
|
||
| it('should render an Inline code button', () => { | ||
| const button = getButtonByIconName('far-code'); | ||
| expect(button).to.exist; | ||
| }); | ||
|
|
||
| it('should render a Blockquote button', () => { | ||
| const button = getButtonByIconName('far-quote-left'); | ||
| expect(button).to.exist; | ||
| }); | ||
|
|
||
| it('should render heading buttons H1–H3', () => { | ||
| expect(getButtonByIconName('far-h1')).to.exist; | ||
| expect(getButtonByIconName('far-h2')).to.exist; | ||
| expect(getButtonByIconName('far-h3')).to.exist; | ||
| }); | ||
|
|
||
| it('should render list buttons', () => { | ||
| expect(getButtonByIconName('far-list')).to.exist; | ||
| expect(getButtonByIconName('far-list-ol')).to.exist; | ||
| }); | ||
|
|
||
| it('should render Undo and Redo buttons', () => { | ||
| expect(getButtonByIconName('far-arrow-rotate-left')).to.exist; | ||
| expect(getButtonByIconName('far-arrow-rotate-right')).to.exist; | ||
| }); | ||
|
|
||
| it('should not be disabled by default', () => { | ||
| expect(toolbar).not.to.have.attribute('disabled'); | ||
| }); | ||
|
|
||
| it('should have Undo disabled when there is no history', () => { | ||
| const undo = getNativeButtonByIconName('far-arrow-rotate-left'); | ||
| expect(undo).to.have.attribute('aria-disabled', 'true'); | ||
| }); | ||
|
|
||
| it('should have Redo disabled when there is nothing to redo', () => { | ||
| const redo = getNativeButtonByIconName('far-arrow-rotate-right'); | ||
| expect(redo).to.have.attribute('aria-disabled', 'true'); | ||
| }); | ||
|
|
||
| it('should not show any format button as active (no selection)', () => { | ||
| const activeButtons = toolbar.renderRoot.querySelectorAll('sl-button[aria-pressed="true"]'); | ||
| expect(activeButtons.length).to.equal(0); | ||
| }); | ||
| }); | ||
|
|
||
| describe('with content', () => { | ||
| beforeEach(async () => { | ||
| editor = await fixture<Editor>(html`<sl-editor .value=${'<p><strong>bold text</strong></p>'}></sl-editor>`); | ||
| toolbar = editor.renderRoot.querySelector('sl-editor-toolbar')!; | ||
| await toolbar.updateComplete; | ||
| }); | ||
|
|
||
| it('should keep Undo disabled with initial content and no history', () => { | ||
| const undo = getButtonByIconName('far-arrow-rotate-left'); | ||
| const nativeUndo = getNativeButtonByIconName('far-arrow-rotate-left'); | ||
| expect(undo).to.exist; | ||
| expect(nativeUndo).to.have.attribute('aria-disabled', 'true'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('active state', () => { | ||
| beforeEach(async () => { | ||
| editor = await fixture<Editor>(html`<sl-editor .value=${'<p><strong>bold</strong></p>'}></sl-editor>`); | ||
| toolbar = editor.renderRoot.querySelector('sl-editor-toolbar')!; | ||
| await toolbar.updateComplete; | ||
| }); | ||
|
|
||
| it('should show Bold button as active when cursor is in bold text', async () => { | ||
| const view = editor.view!; | ||
|
|
||
| // Select the "bold" text by dispatching a selection transaction | ||
| const tr = view.state.tr.setSelection( | ||
| // Select all content inside the paragraph | ||
| TextSelection.near(view.state.doc.resolve(2)) | ||
| ); | ||
| view.dispatch(tr); | ||
| await toolbar.updateComplete; | ||
|
|
||
| const boldButton = getButtonByIconName('editor-bold'); | ||
| expect(boldButton).to.have.attribute('fill', 'outline'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('toolbar disabled', () => { | ||
| beforeEach(async () => { | ||
| editor = await fixture<Editor>(html`<sl-editor disabled></sl-editor>`); | ||
| toolbar = editor.renderRoot.querySelector('sl-editor-toolbar')!; | ||
| await toolbar.updateComplete; | ||
| }); | ||
|
|
||
| it('should be disabled when the editor is disabled', () => { | ||
| expect(toolbar).to.have.attribute('disabled'); | ||
| }); | ||
|
|
||
| it('should pass disabled to the sl-tool-bar', () => { | ||
| const toolBar = toolbar.renderRoot.querySelector('sl-tool-bar'); | ||
| expect(toolBar).to.have.attribute('disabled'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('toolbar hidden', () => { | ||
| beforeEach(async () => { | ||
| editor = await fixture<Editor>(html`<sl-editor .toolbar=${false}></sl-editor>`); | ||
| await editor.updateComplete; | ||
| }); | ||
|
|
||
| it('should not render the toolbar when toolbar=false', () => { | ||
| const t = editor.renderRoot.querySelector('sl-editor-toolbar'); | ||
| expect(t).not.to.exist; | ||
| }); | ||
| }); | ||
|
|
||
| describe('formatting actions', () => { | ||
| beforeEach(async () => { | ||
| editor = await fixture<Editor>(html`<sl-editor .value=${'<p>hello world</p>'}></sl-editor>`); | ||
| toolbar = editor.renderRoot.querySelector('sl-editor-toolbar')!; | ||
| await toolbar.updateComplete; | ||
|
|
||
| // Select all text via ProseMirror | ||
| const { state, dispatch } = editor.view!; | ||
| const { AllSelection } = await import('prosemirror-state'); | ||
| dispatch(state.tr.setSelection(new AllSelection(state.doc))); | ||
| await toolbar.updateComplete; | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| editor.remove(); | ||
| }); | ||
|
|
||
| it('should apply bold when Bold button is clicked', async () => { | ||
| const boldButton = getButtonByIconName('editor-bold')!; | ||
| boldButton.click(); | ||
| await toolbar.updateComplete; | ||
|
|
||
| expect(getButtonByIconName('editor-bold')).to.have.attribute('fill', 'outline'); | ||
| }); | ||
|
|
||
| it('should apply italic when Italic button is clicked', async () => { | ||
| const italicButton = getButtonByIconName('editor-italic')!; | ||
| italicButton.click(); | ||
| await toolbar.updateComplete; | ||
|
|
||
| expect(getButtonByIconName('editor-italic')).to.have.attribute('fill', 'outline'); | ||
| }); | ||
|
|
||
| it('should toggle heading when H1 button is clicked', async () => { | ||
| const h1Button = getButtonByIconName('far-h1')!; | ||
| h1Button.click(); | ||
| await toolbar.updateComplete; | ||
|
|
||
| expect(getButtonByIconName('far-h1')).to.have.attribute('fill', 'outline'); | ||
| }); | ||
|
|
||
| it('should deactivate heading when clicked again', async () => { | ||
| const h1Button = getButtonByIconName('far-h1')!; | ||
| h1Button.click(); | ||
| await toolbar.updateComplete; | ||
| h1Button.click(); | ||
| await toolbar.updateComplete; | ||
|
|
||
| expect(getButtonByIconName('far-h1')).not.to.have.attribute('fill'); | ||
| }); | ||
|
|
||
| it('should keep paragraph block when Blockquote action is not applicable', async () => { | ||
| const view = editor.view!; | ||
| view.dispatch(view.state.tr.setSelection(TextSelection.near(view.state.doc.resolve(2)))); | ||
| await toolbar.updateComplete; | ||
|
|
||
| const bqButton = getButtonByIconName('far-quote-left')!; | ||
| bqButton.click(); | ||
| await toolbar.updateComplete; | ||
|
|
||
| expect(editor.view?.state.doc.firstChild?.type.name).to.equal('paragraph'); | ||
| }); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test asserts that clicking the Blockquote button does not change the document (expects the first node to remain
paragraph). That contradicts the PR description which lists Blockquote as a supported toolbar block format. Once the blockquote command/schema is fixed so the action is applicable, this test should be updated to assert that blockquote is applied/toggled correctly (including the cursor-selection case).