-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
feat: Add IndentConverter and AlignConverter #5814
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
47 changes: 47 additions & 0 deletions
47
packages/plugin-form-builder/src/utilities/lexical/converters/align.ts
This file contains 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,47 @@ | ||
import type { HTMLConverter } from '../types' | ||
|
||
import { convertLexicalNodesToHTML } from '../serializeLexical' | ||
|
||
type Converter = HTMLConverter<any> | ||
|
||
type ConverterFunctionParams = Parameters<Converter['converter']>[0] | ||
|
||
async function convertFunction({ | ||
converters, | ||
node, | ||
parent, | ||
submissionData, | ||
}: ConverterFunctionParams): Promise<string> { | ||
const filteredSelfConverters = converters.filter( | ||
(converter) => converter.converter !== convertFunction, | ||
) | ||
|
||
let text = await convertLexicalNodesToHTML({ | ||
converters: filteredSelfConverters, | ||
lexicalNodes: [node], | ||
parent, | ||
submissionData, | ||
}) | ||
|
||
const styleArray = [] | ||
|
||
if (node.indent) { | ||
styleArray.push('text-align: ' + node.format) | ||
} | ||
|
||
const firstTag = text.slice(0, text.indexOf('>') + 1) | ||
if (styleArray.length > 0) { | ||
if (firstTag.includes('style')) { | ||
text = text.replace('style="', `style="${styleArray.join('; ')};`) | ||
} else { | ||
text = text.replace('>', ` style="${styleArray.join('; ')};">`) | ||
} | ||
} | ||
|
||
return text | ||
} | ||
|
||
export const AlignHTMLConverter: Converter = { | ||
converter: convertFunction, | ||
nodeTypes: ['paragraph', 'heading', 'listitem', 'quote'], | ||
} |
47 changes: 47 additions & 0 deletions
47
packages/plugin-form-builder/src/utilities/lexical/converters/indent.ts
This file contains 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,47 @@ | ||
import type { HTMLConverter } from '../types' | ||
|
||
import { convertLexicalNodesToHTML } from '../serializeLexical' | ||
|
||
type Converter = HTMLConverter<any> | ||
|
||
type ConverterFunctionParams = Parameters<Converter['converter']>[0] | ||
|
||
async function convertFunction({ | ||
converters, | ||
node, | ||
parent, | ||
submissionData, | ||
}: ConverterFunctionParams): Promise<string> { | ||
const filteredSelfConverters = converters.filter( | ||
(converter) => converter.converter !== convertFunction, | ||
) | ||
|
||
let text = await convertLexicalNodesToHTML({ | ||
converters: filteredSelfConverters, | ||
lexicalNodes: [node], | ||
parent, | ||
submissionData, | ||
}) | ||
|
||
const styleArray = [] | ||
|
||
if (node.indent) { | ||
styleArray.push(`padding-inline-start: ${node.indent * 20}px`) | ||
} | ||
|
||
const firstTag = text.slice(0, text.indexOf('>') + 1) | ||
if (styleArray.length > 0) { | ||
if (firstTag.includes('style')) { | ||
text = text.replace('style="', `style="${styleArray.join('; ')}`) | ||
} else { | ||
text = text.replace('>', ` style="${styleArray.join('; ')}">`) | ||
} | ||
} | ||
|
||
return text | ||
} | ||
|
||
export const IndentHTMLConverter: Converter = { | ||
converter: convertFunction, | ||
nodeTypes: ['paragraph', 'heading', 'listitem', 'quote'], | ||
} |
This file contains 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
45 changes: 45 additions & 0 deletions
45
packages/richtext-lexical/src/field/features/converters/html/converter/converters/align.ts
This file contains 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,45 @@ | ||
import type { HTMLConverter } from '../types' | ||
|
||
import { convertLexicalNodesToHTML } from '../index' | ||
|
||
type Converter = HTMLConverter<any> | ||
|
||
type ConverterFunctionParams = Parameters<Converter['converter']>[0] | ||
|
||
async function convertFunction({ | ||
converters, | ||
node, | ||
parent, | ||
}: ConverterFunctionParams): Promise<string> { | ||
const filteredSelfConverters = converters.filter( | ||
(converter) => converter.converter !== convertFunction, | ||
) | ||
|
||
let text = await convertLexicalNodesToHTML({ | ||
converters: filteredSelfConverters, | ||
lexicalNodes: [node], | ||
parent, | ||
}) | ||
|
||
const styleArray = [] | ||
|
||
if (node.indent) { | ||
styleArray.push('text-align: ' + node.format) | ||
} | ||
|
||
const firstTag = text.slice(0, text.indexOf('>') + 1) | ||
if (styleArray.length > 0) { | ||
if (firstTag.includes('style')) { | ||
text = text.replace('style="', `style="${styleArray.join('; ')};`) | ||
} else { | ||
text = text.replace('>', ` style="${styleArray.join('; ')};">`) | ||
} | ||
} | ||
|
||
return text | ||
} | ||
|
||
export const AlignHTMLConverter: Converter = { | ||
converter: convertFunction, | ||
nodeTypes: ['paragraph', 'heading', 'listitem', 'quote'], | ||
} |
45 changes: 45 additions & 0 deletions
45
packages/richtext-lexical/src/field/features/converters/html/converter/converters/indent.ts
This file contains 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,45 @@ | ||
import type { HTMLConverter } from '../types' | ||
|
||
import { convertLexicalNodesToHTML } from '../index' | ||
|
||
type Converter = HTMLConverter<any> | ||
|
||
type ConverterFunctionParams = Parameters<Converter['converter']>[0] | ||
|
||
async function convertFunction({ | ||
converters, | ||
node, | ||
parent, | ||
}: ConverterFunctionParams): Promise<string> { | ||
const filteredSelfConverters = converters.filter( | ||
(converter) => converter.converter !== convertFunction, | ||
) | ||
|
||
let text = await convertLexicalNodesToHTML({ | ||
converters: filteredSelfConverters, | ||
lexicalNodes: [node], | ||
parent, | ||
}) | ||
|
||
const styleArray = [] | ||
|
||
if (node.indent) { | ||
styleArray.push(`padding-inline-start: ${node.indent * 20}px`) | ||
} | ||
|
||
const firstTag = text.slice(0, text.indexOf('>') + 1) | ||
if (styleArray.length > 0) { | ||
if (firstTag.includes('style')) { | ||
text = text.replace('style="', `style="${styleArray.join('; ')}`) | ||
} else { | ||
text = text.replace('>', ` style="${styleArray.join('; ')}">`) | ||
} | ||
} | ||
|
||
return text | ||
} | ||
|
||
export const IndentHTMLConverter: Converter = { | ||
converter: convertFunction, | ||
nodeTypes: ['paragraph', 'heading', 'listitem', 'quote'], | ||
} |
4 changes: 4 additions & 0 deletions
4
packages/richtext-lexical/src/field/features/converters/html/converter/defaultConverters.ts
This file contains 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.
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.
I think it might be better to add all this directly to the paragraph/text/heading/... html converters (for both indent and align handling) instead of creating new HTML converters for these. That way we'd have a more performant and stable solution that doesn't rely on string replacements.
In order to handle this within those converters, we should expose helper functions that can easily be called to generate the style tags
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.
Okay, so instead of creating two converters, I'll create a file in the
richtext-lexical\src\utilities
folder that exports an implementation that replaces the HTML string so that I can use it in the other converter.I'll close this PR and create a new one. Thank you.