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(Designer): Revert - Updated HTML Editor to support newline characters #4807

Merged
merged 3 commits into from
May 8, 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
@@ -0,0 +1,35 @@
import { describe, expect, it } from 'vitest';
import { ValueSegmentType } from '../../../models/parameter';
import { createLiteralValueSegment, createEmptyLiteralValueSegment, removeFirstAndLast } from '../helper';

describe('Helper functions', () => {
it('creates a literal value segment', () => {
const segment = createLiteralValueSegment('test', '1');
expect(segment).toEqual({
id: '1',
type: ValueSegmentType.LITERAL,
value: 'test',
});
});

it('creates an empty literal value segment', () => {
const segment = createEmptyLiteralValueSegment();
expect(segment).toEqual({
id: expect.any(String), // The id is generated by guid(), so we can't know its exact value
type: ValueSegmentType.LITERAL,
value: '',
});
});

it('removes first and last characters from segments', () => {
const segments = [
{ id: '1', type: ValueSegmentType.LITERAL, value: 'test' },
{ id: '2', type: ValueSegmentType.LITERAL, value: 'example' },
];
const result = removeFirstAndLast(segments, 't', 'e');
expect(result).toEqual([
{ id: '1', type: ValueSegmentType.LITERAL, value: 'est' },
{ id: '2', type: ValueSegmentType.LITERAL, value: 'exampl' },
]);
});
});
30 changes: 2 additions & 28 deletions libs/designer-ui/src/lib/editor/base/utils/editorToSegment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ const getChildrenNodesToSegments = (node: ElementNode, segments: ValueSegment[],
export const convertStringToSegments = (
value: string,
nodeMap: Map<string, ValueSegment>,
options?: SegmentParserOptions,
convertSpaceToNewline?: boolean
options?: SegmentParserOptions
): ValueSegment[] => {
if (!value) {
return [];
Expand Down Expand Up @@ -90,28 +89,7 @@ export const convertStringToSegments = (
segmentSoFar += currChar;

if (!isInQuotedString && currChar === '}' && currSegmentType === ValueSegmentType.TOKEN) {
let token: ValueSegment | undefined = undefined;

// removes formatting compatibility issues between nodemap and HTML text in the editor
// when opening an action with an HTML editor
if (convertSpaceToNewline) {
// modifiedSegmentSoFar -> in segmentSoFar, replace spaces with no space
const modifiedSegmentSoFar = removeNewlinesAndSpaces(segmentSoFar);
// for each key in nodeMap
for (const key of nodeMap.keys()) {
// keyNoNewline = key, but replace all newlines with no space
const keyNoNewline = removeNewlinesAndSpaces(key);
// if the nodemap key and modified HTML segment match,
// take the corresponding HTML node in the nodemap
if (keyNoNewline === modifiedSegmentSoFar) {
token = nodeMap.get(key);
break;
}
}
} else {
token = nodeMap.get(segmentSoFar);
}

const token = nodeMap.get(segmentSoFar);
if (token) {
// If remove quotes param is set, remove the quotes from previous and next segments if it's a single token
if (options?.removeSingleTokenQuotesWrapping && doubleQuotesStarted && returnSegments.length > 0) {
Expand Down Expand Up @@ -164,7 +142,3 @@ const collapseLiteralSegments = (segments: ValueSegment[]): void => {
index++;
}
};

const removeNewlinesAndSpaces = (inputStr: string): string => {
return inputStr.replace(/\s+/g, '').replaceAll(/\n/g, '');
};
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ const appendChildrenNode = (
const childNodeFormat = childNode.getFormat();

if (tokensEnabled && nodeMap) {
const contentAsParameter = convertStringToSegments(decodedTextContent, nodeMap, options, true);
const contentAsParameter = convertStringToSegments(decodedTextContent, nodeMap, options);
contentAsParameter.forEach((segment) => {
const tokenNode = createTokenNodeFromSegment(segment, options, nodeMap);
if (tokenNode) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
encodeStringSegmentTokensInLexicalContext,
} from '../../../../editor/base/utils/parsesegments';
import {
canReplaceSpanWithId,
cleanHtmlString,
cleanStyleAttribute,
encodeSegmentValueInLexicalContext,
Expand All @@ -20,7 +19,7 @@ import { OnChangePlugin } from '@lexical/react/LexicalOnChangePlugin';
import type { EditorState, LexicalEditor } from 'lexical';
import { $getRoot } from 'lexical';

interface HTMLChangePluginProps {
export interface HTMLChangePluginProps {
isValuePlaintext: boolean;
setIsSwitchFromPlaintextBlocked: (value: boolean) => void;
setIsValuePlaintext: (isValuePlaintext: boolean) => void;
Expand Down Expand Up @@ -75,7 +74,6 @@ export const convertEditorState = (
// Create a temporary DOM element to parse the HTML string
const tempElement = getDomFromHtmlEditorString(htmlEditorString, nodeMap);

let idValue = '';
// Loop through all elements and remove unwanted attributes
const elements = tempElement.querySelectorAll('*');
// biome-ignore lint/style/useForOf: Node List isn't iterable
Expand All @@ -89,7 +87,7 @@ export const convertEditorState = (
}
if (attribute.name === 'id' && !isValuePlaintext) {
// If we're in the rich HTML editor, encoding occurs at the element level since they are all wrapped in <span>.
idValue = element.getAttribute('id') ?? ''; // e.g., "@{concat('&lt;', '"')}"
const idValue = element.getAttribute('id') ?? ''; // e.g., "@{concat('&lt;', '"')}"
const encodedIdValue = encodeSegmentValueInLexicalContext(idValue); // e.g., "@{concat('%26lt;', '%22')}"
element.setAttribute('id', encodedIdValue);
continue;
Expand All @@ -108,11 +106,12 @@ export const convertEditorState = (

// Replace `<span id="..."></span>` with the captured `id` value if it is found in the viable IDs map.
const spanIdPattern = /<span id="(.*?)"><\/span>/g;
let noTokenSpansString = decodedLexicalString;
const decodedLexicalStringWithoutNewlines = decodedLexicalString.replace(/\n/g, '');
if (canReplaceSpanWithId(idValue, nodeMap)) {
noTokenSpansString = decodedLexicalStringWithoutNewlines.replace(spanIdPattern, idValue);
}
const noTokenSpansString = decodedLexicalString.replace(spanIdPattern, (match, idValue) => {
if (nodeMap.get(idValue)) {
return idValue;
}
return match;
});
const valueSegments: ValueSegment[] = convertStringToSegments(noTokenSpansString, nodeMap, { tokensEnabled: true });
resolve(valueSegments);
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { HTMLChangePlugin } from '../HTMLChangePlugin';
import { test } from 'vitest';

test('HTMLChangePlugin can be used', () => {
const mockProps = {
isValuePlaintext: false,
setIsSwitchFromPlaintextBlocked: () => {},
setIsValuePlaintext: () => {},
setValue: () => {},
};

try {
HTMLChangePlugin(mockProps);
} catch (e) {
throw new Error(`HTMLChangePlugin could not be used: ${e}`);
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,6 @@ export const cleanHtmlString = (html: string): string => {
return cleanedHtmlString;
};

// If we can find the id in the nodemap, return true.
export const canReplaceSpanWithId = (idValue: string, nodeMap: Map<string, ValueSegment>): boolean => {
return nodeMap.get(idValue) !== undefined;
};

export const cleanStyleAttribute = (styleAttributeValue: string): string | undefined => {
const newValue = styleAttributeValue.replace('white-space: pre-wrap;', '').trim();
return newValue.length ? newValue : undefined;
Expand Down
Loading