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

feat: Add IndentConverter and AlignConverter #5814

Closed
wants to merge 2 commits into from
Closed
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
@@ -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'],
}
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('; ')}`)
Copy link
Member

@AlessioGr AlessioGr Sep 1, 2024

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

Copy link
Contributor Author

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.

} else {
text = text.replace('>', ` style="${styleArray.join('; ')}">`)
}
}

return text
}

export const IndentHTMLConverter: Converter = {
converter: convertFunction,
nodeTypes: ['paragraph', 'heading', 'listitem', 'quote'],
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import type { HTMLConverter } from './types'

import { AlignHTMLConverter } from './converters/align'
import { HeadingHTMLConverter } from './converters/heading'
import { IndentHTMLConverter } from './converters/indent'
import { LinebreakHTMLConverter } from './converters/linebreak'
import { LinkHTMLConverter } from './converters/link'
import { ListHTMLConverter, ListItemHTMLConverter } from './converters/list'
Expand All @@ -9,6 +11,8 @@ import { QuoteHTMLConverter } from './converters/quote'
import { TextHTMLConverter } from './converters/text'

export const defaultHTMLConverters: HTMLConverter[] = [
AlignHTMLConverter,
IndentHTMLConverter,
ParagraphHTMLConverter,
TextHTMLConverter,
LinebreakHTMLConverter,
Expand Down
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'],
}
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'],
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import type { HTMLConverter } from './types'

import { AlignHTMLConverter } from './converters/align'
import { IndentHTMLConverter } from './converters/indent'
import { LinebreakHTMLConverter } from './converters/linebreak'
import { ParagraphHTMLConverter } from './converters/paragraph'
import { TextHTMLConverter } from './converters/text'

export const defaultHTMLConverters: HTMLConverter[] = [
AlignHTMLConverter,
IndentHTMLConverter,
ParagraphHTMLConverter,
TextHTMLConverter,
LinebreakHTMLConverter,
Expand Down
Loading