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

Live Markdown for web refactor #394

Open
wants to merge 69 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
69 commits
Select commit Hold shift + click to select a range
f853061
Add refactor changes
Skalakid Jun 25, 2024
c4108c1
Fix parserUtils
Skalakid Jun 25, 2024
38693c4
Add tree building when creating markdown HTML structure
Skalakid Jun 26, 2024
6a1eaf1
Refactor function and type names
Skalakid Jun 26, 2024
75d6afd
Fix TS errors
Skalakid Jun 26, 2024
db68b78
Update parser structure
Skalakid Jun 26, 2024
31711ab
Move BrowserUtils into an object
Skalakid Jun 26, 2024
cab8ca7
Move utils to separate folder
Skalakid Jun 26, 2024
2f829cc
Add block utils
Skalakid Jun 26, 2024
b9b2984
Move functions from above the web component to utils
Skalakid Jun 26, 2024
3fbc456
Fix unit tests
Skalakid Jun 27, 2024
d74e0e4
Fix cursor positioning bugs
Skalakid Jun 28, 2024
220395b
Remove scrollCursorIntoView function
Skalakid Jun 28, 2024
4d95382
Rename variable names
Skalakid Jun 28, 2024
907c73b
Replace textContent with ref value
Skalakid Jun 28, 2024
1017ebc
Merge branch 'main' into @Skalakid/web-parser-refactor
Skalakid Jun 28, 2024
1f1fefd
Merge branch 'main' into @Skalakid/web-parser-refactor
Skalakid Jul 1, 2024
ff41dd7
Fix crashes and cursor positioning in E/App
Skalakid Jul 1, 2024
d1d8a49
Fix copying and pasting text with markdown
Skalakid Jul 1, 2024
153ead8
Fix pasting text starting with newlines
Skalakid Jul 1, 2024
ea6aad8
Fix errors when replacing text
Skalakid Jul 2, 2024
87953bb
Fix HTML injestions and pasted text parsing
Skalakid Jul 2, 2024
3d30621
Fix paste text trimming condiftion
Skalakid Jul 2, 2024
471f53a
Fix cutting text
Skalakid Jul 2, 2024
92a224c
Change handlePaste logic to fix newlines in pasted text
Skalakid Jul 2, 2024
c14add4
Fix cursor positioning when undoing/redoing previously pasted text
Skalakid Jul 2, 2024
e9d8ec3
Fix text coloring
Skalakid Jul 2, 2024
e24d259
Add review changes
Skalakid Jul 3, 2024
092775d
Move updateTextColor function
Skalakid Jul 3, 2024
a0c1dfa
Fix cursor positioning when changing text and styles at the same time
Skalakid Jul 3, 2024
d04ad32
Fix newlines on FireFox
Skalakid Jul 3, 2024
63de148
Fix cursor position value update when entering newline inside codeblock
Skalakid Jul 3, 2024
f06d10f
Fix removing characters when cursor is at the beginning of the line o…
Skalakid Jul 3, 2024
34eabe2
Fix writing in empty line on Firefox
Skalakid Jul 3, 2024
a52e3f5
Fix cursor position value on Cmd+A on FireFox
Skalakid Jul 3, 2024
b5515cf
Fix getting value in e2e tests
Skalakid Jul 4, 2024
5a043a3
Fix input e2e tests
Skalakid Jul 4, 2024
5a1dae2
Fix style e2e tests
Skalakid Jul 4, 2024
60f4f4b
Fix text manipulation e2e tests
Skalakid Jul 4, 2024
55e78b4
Update checkCursorPosition function in e2e tests
Skalakid Jul 4, 2024
8a0a7be
Update checkCursorPosition function in e2e tests
Skalakid Jul 4, 2024
96a9b5c
Fix cursor position after redoing pasted text
Skalakid Jul 5, 2024
8c85473
Merge branch '@Skalakid/web-parser-refactor' of github.com:Expensify/…
Skalakid Jul 5, 2024
401febe
Merge branch 'main' into @Skalakid/web-parser-refactor
Skalakid Jul 5, 2024
862e74a
Fix e2e tests on CI/CD
Skalakid Jul 5, 2024
0580f45
Uncomment undo test
Skalakid Jul 5, 2024
6682c9d
Fix TS errors
Skalakid Jul 5, 2024
8165fb9
Change line merging funciton
Skalakid Jul 5, 2024
9e2b943
Fix selection event sending on paste
Skalakid Jul 5, 2024
0f5b86a
Merge branch 'main' into @Skalakid/web-parser-refactor
Skalakid Jul 8, 2024
5f369ec
Fix scrolling cursor into view on Safari browser
Skalakid Jul 8, 2024
7cdaf9a
Merge branch 'main' into @Skalakid/web-parser-refactor
Skalakid Jul 9, 2024
6bada78
Enhance cursor positioning on input
Skalakid Jul 9, 2024
d9c097d
Merge branch 'main' into @Skalakid/web-parser-refactor
Skalakid Jul 10, 2024
309bccf
Fix diacritics after CMD+A
Skalakid Jul 10, 2024
90d2ea3
Fix autocorrect cursor positioning
Skalakid Jul 10, 2024
111fd6b
Fix deleting codeBlock lines with CMD+backspace
Skalakid Jul 10, 2024
d14f4ba
Fix text color on undo/redo
Skalakid Jul 10, 2024
82d58ca
Fix removing last letter from the line
Skalakid Jul 10, 2024
2b9e56a
Fix cursor position when replacing text with the same text
Skalakid Jul 11, 2024
5861a21
Change parseInnerHTMLToText function
Skalakid Jul 15, 2024
68ae9be
Fix input behavior when interracting with display: block element
Skalakid Jul 15, 2024
0bc882a
Fix getTreeNodeByIndex function
Skalakid Jul 16, 2024
cc8e307
Fix replacing whole content of the input
Skalakid Jul 16, 2024
3da3989
Fix set cursor position when content changes
Skalakid Jul 16, 2024
ccb251e
Fix removing selection on paste
Skalakid Jul 16, 2024
e759170
Fix set cursor position on paste
Skalakid Jul 16, 2024
4db4441
Fix dissapearing cursor bug
Skalakid Jul 16, 2024
0480cb8
Fix pasting text into empty input
Skalakid Jul 16, 2024
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
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ module.exports = {
root: true,
rules: {
'rulesdir/prefer-underscore-method': 'off',
'rulesdir/prefer-import-module-contents': 'off',
'react/jsx-props-no-spreading': 'off',
'react/require-default-props': 'off',
'react/jsx-filename-extension': ['error', { extensions: ['.tsx', '.jsx'] }],
Expand Down
12 changes: 6 additions & 6 deletions WebExample/__tests__/input.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {test, expect} from '@playwright/test';
import * as TEST_CONST from '../../example/src/testConstants';
import {checkCursorPosition, setupInput} from './utils';
import {getCursorPosition, getElementValue, setupInput} from './utils';

test.beforeEach(async ({page}) => {
await page.goto(TEST_CONST.LOCAL_URL, {waitUntil: 'load'});
Expand All @@ -12,8 +12,8 @@ test.describe('typing', () => {

await inputLocator.focus();
await inputLocator.pressSequentially(TEST_CONST.EXAMPLE_CONTENT);
const value = await inputLocator.innerText();
expect(value).toEqual(TEST_CONST.EXAMPLE_CONTENT);

expect(await getElementValue(inputLocator)).toEqual(TEST_CONST.EXAMPLE_CONTENT);
});

test('fast type cursor position', async ({page}) => {
Expand All @@ -23,10 +23,10 @@ test.describe('typing', () => {

await inputLocator.pressSequentially(EXAMPLE_LONG_CONTENT);

expect(await inputLocator.innerText()).toBe(EXAMPLE_LONG_CONTENT);
expect(await getElementValue(inputLocator)).toBe(EXAMPLE_LONG_CONTENT);

const cursorPosition = await page.evaluate(checkCursorPosition);
const cursorPosition = await getCursorPosition(inputLocator);

expect(cursorPosition).toBe(EXAMPLE_LONG_CONTENT.length);
expect(cursorPosition.end).toBe(EXAMPLE_LONG_CONTENT.length);
});
});
36 changes: 19 additions & 17 deletions WebExample/__tests__/textManipulation.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {test, expect} from '@playwright/test';
import type {Locator, Page} from '@playwright/test';
import * as TEST_CONST from '../../example/src/testConstants';
import {checkCursorPosition, setupInput, getElementStyle, pressCmd} from './utils';
import {getCursorPosition, setupInput, getElementStyle, pressCmd, getElementValue} from './utils';

const pasteContent = async ({text, page, inputLocator}: {text: string; page: Page; inputLocator: Locator}) => {
await page.evaluate(async (pasteText) => navigator.clipboard.writeText(pasteText), text);
Expand Down Expand Up @@ -43,7 +43,7 @@ test.describe('paste content', () => {
const newText = '*bold*';
await pasteContent({text: newText, page, inputLocator});

expect(await inputLocator.innerText()).toBe(newText);
expect(await getElementValue(inputLocator)).toBe(newText);
});

test('paste undo', async ({page, browserName}) => {
Expand All @@ -61,10 +61,9 @@ test.describe('paste content', () => {
await page.evaluate(async (pasteText) => navigator.clipboard.writeText(pasteText), PASTE_TEXT_SECOND);
await pressCmd({inputLocator, command: 'v'});
await page.waitForTimeout(TEST_CONST.INPUT_HISTORY_DEBOUNCE_TIME_MS);

await pressCmd({inputLocator, command: 'z'});

expect(await inputLocator.innerText()).toBe(PASTE_TEXT_FIRST);
await page.waitForTimeout(TEST_CONST.INPUT_HISTORY_DEBOUNCE_TIME_MS);
expect(await getElementValue(inputLocator)).toBe(PASTE_TEXT_FIRST);
});

test('paste redo', async ({page}) => {
Expand All @@ -84,7 +83,7 @@ test.describe('paste content', () => {
await pressCmd({inputLocator, command: 'z'});
await pressCmd({inputLocator, command: 'Shift+z'});

expect(await inputLocator.innerText()).toBe(`${PASTE_TEXT_FIRST}${PASTE_TEXT_SECOND}`);
expect(await getElementValue(inputLocator)).toBe(`${PASTE_TEXT_FIRST}${PASTE_TEXT_SECOND}`);
});
});

Expand All @@ -93,9 +92,9 @@ test('select all', async ({page}) => {
await inputLocator.focus();
await pressCmd({inputLocator, command: 'a'});

const cursorPosition = await page.evaluate(checkCursorPosition);
const cursorPosition = await getCursorPosition(inputLocator);

expect(cursorPosition).toBe(TEST_CONST.EXAMPLE_CONTENT.length);
expect(cursorPosition.end).toBe(TEST_CONST.EXAMPLE_CONTENT.length);
});

test('cut content changes', async ({page, browserName}) => {
Expand All @@ -107,15 +106,12 @@ test('cut content changes', async ({page, browserName}) => {

const inputLocator = await setupInput(page, 'clear');
await pasteContent({text: WRAPPED_CONTENT, page, inputLocator});
const rootHandle = await inputLocator.locator('span.root').first();

await page.evaluate(async (initialContent) => {
const filteredNode = Array.from(document.querySelectorAll('div[contenteditable="true"] > span.root span')).find((node) => {
return node.textContent?.includes(initialContent) && node.nextElementSibling && node.nextElementSibling.textContent?.includes('*');
});
await page.evaluate(async () => {
const filteredNode = Array.from(document.querySelectorAll('span[data-type="text"]'));

const startNode = filteredNode;
const endNode = filteredNode?.nextElementSibling;
const startNode = filteredNode[1];
const endNode = filteredNode[2];

if (startNode?.firstChild && endNode?.lastChild) {
const range = new Range();
Expand All @@ -126,10 +122,16 @@ test('cut content changes', async ({page, browserName}) => {
selection?.removeAllRanges();
selection?.addRange(range);
}
}, INITIAL_CONTENT);

return filteredNode;
});

await inputLocator.focus();
await pressCmd({inputLocator, command: 'x'});

expect(await rootHandle.innerHTML()).toBe(EXPECTED_CONTENT);
expect(await getElementValue(inputLocator)).toBe(EXPECTED_CONTENT);

// Ckeck if there is no markdown elements after the cut operation
const spans = await inputLocator.locator('span[data-type="text"]');
expect(await spans.count()).toBe(1);
});
29 changes: 16 additions & 13 deletions WebExample/__tests__/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,10 @@ const setupInput = async (page: Page, action?: 'clear' | 'reset') => {
return inputLocator;
};

const checkCursorPosition = () => {
const editableDiv = document.querySelector('div[contenteditable="true"]') as HTMLElement;
const range = window.getSelection()?.getRangeAt(0);
if (!range || !editableDiv) {
return null;
}
const preCaretRange = range.cloneRange();
preCaretRange.selectNodeContents(editableDiv);
preCaretRange.setEnd(range.endContainer, range.endOffset);
return preCaretRange.toString().length;
const getCursorPosition = async (elementHandle: Locator) => {
const inputSelectionHandle = await elementHandle.evaluateHandle((div: HTMLInputElement) => ({start: div.selectionStart, end: div.selectionEnd}));
const selection = await inputSelectionHandle.jsonValue();
return selection;
};

const setCursorPosition = ({startNode, endNode}: {startNode?: Element; endNode?: Element | null}) => {
Expand All @@ -43,8 +37,11 @@ const getElementStyle = async (elementHandle: Locator) => {

if (elementHandle) {
await elementHandle.waitFor({state: 'attached'});

elementStyle = await elementHandle.getAttribute('style');
// We need to get styles from the parent element because every text node is wrapped additionally with a span element
const parentElementHandle = await elementHandle.evaluateHandle((element) => {
return element.parentElement;
});
elementStyle = await parentElementHandle.asElement()?.getAttribute('style');
}
return elementStyle;
};
Expand All @@ -55,4 +52,10 @@ const pressCmd = async ({inputLocator, command}: {inputLocator: Locator; command
await inputLocator.press(`${OPERATION_MODIFIER}+${command}`);
};

export {setupInput, checkCursorPosition, setCursorPosition, getElementStyle, pressCmd};
const getElementValue = async (elementHandle: Locator) => {
const inputValueHandle = await elementHandle.evaluateHandle((div: HTMLInputElement) => div.value);
const value = await inputValueHandle.jsonValue();
return value;
};

export {setupInput, getCursorPosition, setCursorPosition, getElementStyle, pressCmd, getElementValue};
2 changes: 0 additions & 2 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import * as React from 'react';

import {Button, Platform, StyleSheet, Text, View} from 'react-native';

import {MarkdownTextInput} from '@expensify/react-native-live-markdown';
import type {TextInput} from 'react-native';
import * as TEST_CONST from './testConstants';
Expand Down
6 changes: 3 additions & 3 deletions parser/__tests__/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import {expect} from '@jest/globals';
import type * as ParserTypes from '../index';
import type {Range} from '../index';

require('../react-native-live-markdown-parser.js');

declare module 'expect' {
interface Matchers<R> {
toBeParsedAs(expectedRanges: ParserTypes.Range[]): R;
toBeParsedAs(expectedRanges: Range[]): R;
}
}

const toBeParsedAs = function (actual: string, expectedRanges: ParserTypes.Range[]) {
const toBeParsedAs = function (actual: string, expectedRanges: Range[]) {
const actualRanges = global.parseExpensiMarkToRanges(actual);
if (JSON.stringify(actualRanges) !== JSON.stringify(expectedRanges)) {
return {
Expand Down
12 changes: 6 additions & 6 deletions parser/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// eslint-disable-next-line import/no-unresolved
import ExpensiMark from 'expensify-common/dist/ExpensiMark';
import * as Utils from './utils';
import {unescapeText} from './utils';

type MarkdownType = 'bold' | 'italic' | 'strikethrough' | 'emoji' | 'mention-here' | 'mention-user' | 'mention-report' | 'link' | 'code' | 'pre' | 'blockquote' | 'h1' | 'syntax';
type Range = {
Expand Down Expand Up @@ -49,7 +49,7 @@ function parseTokensToTree(tokens: Token[]): StackItem {
const stack: StackItem[] = [{tag: '<>', children: []}];
tokens.forEach(([type, payload]) => {
if (type === 'TEXT') {
const text = Utils.unescapeText(payload);
const text = unescapeText(payload);
const top = stack[stack.length - 1];
top!.children.push(text);
} else if (type === 'HTML') {
Expand Down Expand Up @@ -160,10 +160,10 @@ function parseTreeToTextAndRanges(tree: StackItem): [string, Range[]] {
appendSyntax('```');
} else if (node.tag.startsWith('<a href="')) {
const rawHref = node.tag.match(/href="([^"]*)"/)![1]!; // always present
const href = Utils.unescapeText(rawHref);
const href = unescapeText(rawHref);
const isLabeledLink = node.tag.match(/data-link-variant="([^"]*)"/)![1] === 'labeled';
const dataRawHref = node.tag.match(/data-raw-href="([^"]*)"/);
const matchString = dataRawHref ? Utils.unescapeText(dataRawHref[1]!) : href;
const matchString = dataRawHref ? unescapeText(dataRawHref[1]!) : href;
if (!isLabeledLink && node.children.length === 1 && typeof node.children[0] === 'string' && (node.children[0] === matchString || `mailto:${node.children[0]}` === href)) {
addChildrenWithStyle(node.children[0], 'link');
} else {
Expand All @@ -178,12 +178,12 @@ function parseTreeToTextAndRanges(tree: StackItem): [string, Range[]] {
const alt = node.tag.match(/alt="([^"]*)"/);
const hasAlt = node.tag.match(/data-link-variant="([^"]*)"/)![1] === 'labeled';
const rawLink = node.tag.match(/data-raw-href="([^"]*)"/);
const linkString = rawLink ? Utils.unescapeText(rawLink[1]!) : src;
const linkString = rawLink ? unescapeText(rawLink[1]!) : src;

appendSyntax('!');
if (hasAlt) {
appendSyntax('[');
processChildren(Utils.unescapeText(alt?.[1] || ''));
processChildren(unescapeText(alt?.[1] || ''));
appendSyntax(']');
}
appendSyntax('(');
Expand Down
11 changes: 4 additions & 7 deletions src/MarkdownTextInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,15 @@ import {StyleSheet, TextInput, processColor} from 'react-native';
import React from 'react';
import type {TextInputProps} from 'react-native';
import MarkdownTextInputDecoratorViewNativeComponent from './MarkdownTextInputDecoratorViewNativeComponent';
import type {MarkdownStyle} from './MarkdownTextInputDecoratorViewNativeComponent';
import NativeLiveMarkdownModule from './NativeLiveMarkdownModule';
import type * as MarkdownTextInputDecoratorViewNativeComponentTypes from './MarkdownTextInputDecoratorViewNativeComponent';
import * as StyleUtils from './styleUtils';
import type * as StyleUtilsTypes from './styleUtils';
import {mergeMarkdownStyleWithDefault} from './styleUtils';
import type {PartialMarkdownStyle} from './styleUtils';

if (NativeLiveMarkdownModule) {
NativeLiveMarkdownModule.install();
}

type PartialMarkdownStyle = StyleUtilsTypes.PartialMarkdownStyle;
type MarkdownStyle = MarkdownTextInputDecoratorViewNativeComponentTypes.MarkdownStyle;

interface MarkdownTextInputProps extends TextInputProps {
markdownStyle?: PartialMarkdownStyle;
}
Expand All @@ -36,7 +33,7 @@ function processColorsInMarkdownStyle(input: MarkdownStyle): MarkdownStyle {
}

function processMarkdownStyle(input: PartialMarkdownStyle | undefined): MarkdownStyle {
return processColorsInMarkdownStyle(StyleUtils.mergeMarkdownStyleWithDefault(input));
return processColorsInMarkdownStyle(mergeMarkdownStyleWithDefault(input));
}

const MarkdownTextInput = React.forwardRef<TextInput, MarkdownTextInputProps>((props, ref) => {
Expand Down
Loading
Loading