Skip to content
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

Force add text align styles when a defaultAlignment is set #5891

Merged
merged 8 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/plenty-readers-sin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@tiptap/extension-text-align": patch
---

Fixed an issue that caused defaultAlignment options not being added as text-styles to elements. defaultAlignment is now nullable to **disable the enforced text-align styles**.
15 changes: 11 additions & 4 deletions demos/src/Extensions/TextAlign/React/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,20 @@ context('/src/Extensions/TextAlign/React/', () => {
})
})

it('should parse left align text correctly (and not render)', () => {
it('should parse a null alignment correctly', () => {
cy.get('.tiptap').then(([{ editor }]) => {
editor.commands.setContent('<p style="text-align: left">Example Text</p>')
editor.commands.setContent('<p>Example Text</p>')
expect(editor.getHTML()).to.eq('<p>Example Text</p>')
})
})

it('should parse left align text correctly', () => {
cy.get('.tiptap').then(([{ editor }]) => {
editor.commands.setContent('<p style="text-align: left">Example Text</p>')
expect(editor.getHTML()).to.eq('<p style="text-align: left">Example Text</p>')
})
})

it('should parse center align text correctly', () => {
cy.get('.tiptap').then(([{ editor }]) => {
editor.commands.setContent('<p style="text-align: center">Example Text</p>')
Expand Down Expand Up @@ -55,7 +62,7 @@ context('/src/Extensions/TextAlign/React/', () => {
it('aligns the text left on the 1st button', () => {
cy.get('button:nth-child(1)').click()

cy.get('.tiptap').find('p').should('not.have.css', 'text-align', 'left')
cy.get('.tiptap').find('p').should('have.css', 'text-align', 'left')
})

it('aligns the text center on the 2nd button', () => {
Expand Down Expand Up @@ -86,7 +93,7 @@ context('/src/Extensions/TextAlign/React/', () => {
cy.get('.tiptap')
.trigger('keydown', { modKey: true, shiftKey: true, key: 'l' })
.find('p')
.should('not.have.css', 'text-align', 'left')
.should('have.css', 'text-align', 'left')
})

it('aligns the text center when pressing the keyboard shortcut', () => {
Expand Down
13 changes: 10 additions & 3 deletions demos/src/Extensions/TextAlign/Vue/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,17 @@ context('/src/Extensions/TextAlign/Vue/', () => {
})
})

it('should parse a null alignment correctly', () => {
cy.get('.tiptap').then(([{ editor }]) => {
editor.commands.setContent('<p>Example Text</p>')
expect(editor.getHTML()).to.eq('<p>Example Text</p>')
})
})

it('should parse left align text correctly (and not render)', () => {
cy.get('.tiptap').then(([{ editor }]) => {
editor.commands.setContent('<p style="text-align: left">Example Text</p>')
expect(editor.getHTML()).to.eq('<p>Example Text</p>')
expect(editor.getHTML()).to.eq('<p style="text-align: left">Example Text</p>')
})
})

Expand Down Expand Up @@ -58,7 +65,7 @@ context('/src/Extensions/TextAlign/Vue/', () => {

cy.get('.tiptap')
.find('p')
.should('not.have.css', 'text-align', 'left')
.should('have.css', 'text-align', 'left')
})

it('aligns the text center on the 2nd button', () => {
Expand Down Expand Up @@ -101,7 +108,7 @@ context('/src/Extensions/TextAlign/Vue/', () => {
cy.get('.tiptap')
.trigger('keydown', { modKey: true, shiftKey: true, key: 'l' })
.find('p')
.should('not.have.css', 'text-align', 'left')
.should('have.css', 'text-align', 'left')
})

it('aligns the text center when pressing the keyboard shortcut', () => {
Expand Down
8 changes: 4 additions & 4 deletions packages/extension-text-align/src/text-align.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export interface TextAlignOptions {
* @default 'left'
* @example 'center'
*/
defaultAlignment: string,
defaultAlignment: string | null,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we be stricter on the types here, can only be left, right & center, correct?

Same for alignments too

}

declare module '@tiptap/core' {
Expand Down Expand Up @@ -52,7 +52,7 @@ export const TextAlign = Extension.create<TextAlignOptions>({
return {
types: [],
alignments: ['left', 'center', 'right', 'justify'],
defaultAlignment: 'left',
defaultAlignment: null,
}
},

Expand All @@ -64,12 +64,12 @@ export const TextAlign = Extension.create<TextAlignOptions>({
textAlign: {
default: this.options.defaultAlignment,
parseHTML: element => {
const alignment = element.style.textAlign || this.options.defaultAlignment
const alignment = element.style.textAlign

return this.options.alignments.includes(alignment) ? alignment : this.options.defaultAlignment
},
renderHTML: attributes => {
if (attributes.textAlign === this.options.defaultAlignment) {
if (!attributes.textAlign) {
return {}
}

Expand Down