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

fix(richtext-lexical): preserve indent and text-align when converting Lexical <-> HTML #9165

Merged
merged 4 commits into from
Nov 21, 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
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,13 @@ export const HeadingHTMLConverter: HTMLConverter<any> = {
},
submissionData,
})

return '<' + node?.tag + '>' + childrenText + '</' + node?.tag + '>'
const style = [
node.format ? `text-align: ${node.format};` : '',
node.indent > 0 ? `padding-inline-start: ${node.indent * 40}px;` : '',
]
.filter(Boolean)
.join(' ')
return `<${node?.tag}${style ? ` style='${style}'` : ''}>${childrenText}</${node?.tag}>`
},
nodeTypes: ['heading'],
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,14 @@ export const QuoteHTMLConverter: HTMLConverter<any> = {
},
submissionData,
})
const style = [
node.format ? `text-align: ${node.format};` : '',
node.indent > 0 ? `padding-inline-start: ${node.indent * 40}px;` : '',
]
.filter(Boolean)
.join(' ')

return `<blockquote>${childrenText}</blockquote>`
return `<blockquote${style ? ` style='${style}'` : ''}>${childrenText}</blockquote>`
},
nodeTypes: ['quote'],
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,14 @@ export const BlockquoteFeature = createServerFeature({
req,
showHiddenFields,
})
const style = [
node.format ? `text-align: ${node.format};` : '',
node.indent > 0 ? `padding-inline-start: ${node.indent * 40}px;` : '',
]
.filter(Boolean)
.join(' ')

return `<blockquote>${childrenText}</blockquote>`
return `<blockquote${style ? ` style='${style}'` : ''}>${childrenText}</blockquote>`
},
nodeTypes: [QuoteNode.getType()],
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,13 @@ export const ParagraphHTMLConverter: HTMLConverter<SerializedParagraphNode> = {
req,
showHiddenFields,
})
return `<p>${childrenText}</p>`
const style = [
node.format ? `text-align: ${node.format};` : '',
node.indent > 0 ? `padding-inline-start: ${node.indent * 40}px;` : '',
]
.filter(Boolean)
.join(' ')
return `<p${style ? ` style='${style}'` : ''}>${childrenText}</p>`
},
nodeTypes: ['paragraph'],
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,13 @@ export const HeadingFeature = createServerFeature<
req,
showHiddenFields,
})

return '<' + node?.tag + '>' + childrenText + '</' + node?.tag + '>'
const style = [
node.format ? `text-align: ${node.format};` : '',
node.indent > 0 ? `padding-inline-start: ${node.indent * 40}px;` : '',
]
.filter(Boolean)
.join(' ')
return `<${node?.tag}${style ? ` style='${style}'` : ''}>${childrenText}</${node?.tag}>`
},
nodeTypes: [HeadingNode.getType()],
},
Expand Down
28 changes: 28 additions & 0 deletions test/fields/collections/Lexical/e2e/main/e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -954,6 +954,34 @@ describe('lexicalMain', () => {
})
})

// https://github.com/payloadcms/payload/issues/5146
test('Preserve indent and text-align when converting Lexical <-> HTML', async () => {
await page.goto('http://localhost:3000/admin/collections/rich-text-fields?limit=10')
await page.getByLabel('Create new Rich Text Field').click()
await page.getByLabel('Title*').click()
await page.getByLabel('Title*').fill('Indent and Text-align')
await page.getByRole('paragraph').nth(1).click()
await context.grantPermissions(['clipboard-read', 'clipboard-write'])
const htmlContent = `<p style='text-align: center;'>paragraph centered</p><h1 style='text-align: right;'>Heading right</h1><p>paragraph without indent</p><p style='padding-inline-start: 40px;'>paragraph indent 1</p><h2 style='padding-inline-start: 80px;'>heading indent 2</h2><blockquote style='padding-inline-start: 120px;'>quote indent 3</blockquote>`
await page.evaluate(
async ([htmlContent]) => {
const blob = new Blob([htmlContent], { type: 'text/html' })
const clipboardItem = new ClipboardItem({ 'text/html': blob })
await navigator.clipboard.write([clipboardItem])
},
[htmlContent],
)
// eslint-disable-next-line playwright/no-conditional-in-test
const pasteKey = process.platform === 'darwin' ? 'Meta' : 'Control'
await page.keyboard.press(`${pasteKey}+v`)
await page.locator('#field-richText').click()
await page.locator('#field-richText').fill('asd')
await page.getByRole('button', { name: 'Save' }).click()
await page.getByRole('link', { name: 'API' }).click()
const htmlOutput = page.getByText(htmlContent)
await expect(htmlOutput).toBeVisible()
})

describe('localization', () => {
test.skip('ensure simple localized lexical field works', async () => {
await navigateToLexicalFields(true, true)
Expand Down
Loading