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

Merged
merged 82 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
82 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
5faac50
Fix newline deletion
Skalakid Jul 17, 2024
14d0eed
Improve parseInnerHTMLToText function and fix pasted text correct val…
Skalakid Jul 18, 2024
0c204f5
Remove buildTree function
Skalakid Jul 18, 2024
de12aca
Fix tests
Skalakid Jul 18, 2024
8648205
Replacing text by text cursor position
Skalakid Jul 19, 2024
eb1502a
Fix cursor positioning on custom text pasting (E/App)
Skalakid Jul 19, 2024
3486303
Add function comments
Skalakid Jul 19, 2024
a1bf047
fix: windows emoji picker selection
BartoszGrajdek Aug 2, 2024
457f42b
Merge branch 'main' into @Skalakid/web-parser-refactor
Skalakid Aug 19, 2024
4f76598
Fix app crashes on Safari after sending the message with emoji
Skalakid Aug 19, 2024
ea8c204
Fix replacing multiline text by pasting content
Skalakid Aug 20, 2024
4e895e9
Add text normalization to fix problems with \r\n
Skalakid Aug 20, 2024
077e387
Merge branch 'main' into @Skalakid/web-parser-refactor
Skalakid Aug 21, 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
Prev Previous commit
Next Next commit
Merge branch 'main' into @Skalakid/web-parser-refactor
  • Loading branch information
Skalakid committed Aug 19, 2024
commit 457f42bb52ea8f2263fe54ac861faeab98023c40
36 changes: 7 additions & 29 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@ jobs:
if: ${{ github.actor != 'OSBotify' }}

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
with:
ref: main
# The OS_BOTIFY_COMMIT_TOKEN is a personal access token tied to osbotify
# This is a workaround to allow pushes to a protected branch
token: ${{ secrets.OS_BOTIFY_COMMIT_TOKEN }}

- name: Decrypt & Import OSBotify GPG key
run: |
Expand All @@ -36,17 +39,11 @@ jobs:
git config --global user.name OSBotify
git config --global user.email infra+osbotify@expensify.com

- uses: actions/setup-node@v3
- uses: actions/setup-node@v4
with:
node-version: '16.x'
node-version-file: '.nvmrc'
registry-url: 'https://registry.npmjs.org'

- name: Generate branch name
run: echo "BRANCH_NAME=OSBotify-bump-version-$(uuidgen)" >> $GITHUB_ENV

- name: Create branch for version-bump pull request
run: git checkout -b ${{ env.BRANCH_NAME }}

- name: Install yarn packages
run: yarn install --immutable

Expand All @@ -63,26 +60,7 @@ jobs:
run: git tag ${{ env.NEW_VERSION }}

- name: Push branch and publish tags
run: git push --set-upstream origin ${{ env.BRANCH_NAME }} && git push --tags

- name: Create pull request
run: |
gh pr create \
--title "Update version to ${{ env.NEW_VERSION }}" \
--body "Update version to ${{ env.NEW_VERSION }}"
sleep 5
env:
GITHUB_TOKEN: ${{ secrets.OS_BOTIFY_TOKEN }}

- name: Auto-approve pull request
run: gh pr review --approve ${{ env.BRANCH_NAME }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Auto-merge pull request
run: gh pr merge --squash --delete-branch ${{ env.BRANCH_NAME }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: git push --set-upstream origin main && git push --tags

- name: Build package
run: yarn pack
Expand Down
9 changes: 8 additions & 1 deletion ios/RCTBaseTextInputView+Markdown.mm
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,14 @@ - (BOOL)markdown_textOf:(NSAttributedString *)newText equals:(NSAttributedString
{
RCTMarkdownUtils *markdownUtils = [self getMarkdownUtils];
if (markdownUtils != nil) {
return [newText isEqualToAttributedString:oldText];
// Emoji characters are automatically assigned an AppleColorEmoji NSFont and the original font is moved to NSOriginalFont
// We need to remove these attributes before comparison
NSMutableAttributedString *newTextCopy = [newText mutableCopy];
NSMutableAttributedString *oldTextCopy = [oldText mutableCopy];
[newTextCopy removeAttribute:@"NSFont" range:NSMakeRange(0, newTextCopy.length)];
[oldTextCopy removeAttribute:@"NSFont" range:NSMakeRange(0, oldTextCopy.length)];
[oldTextCopy removeAttribute:@"NSOriginalFont" range:NSMakeRange(0, oldTextCopy.length)];
return [newTextCopy isEqualToAttributedString:oldTextCopy];
}

return [self markdown_textOf:newText equals:oldText];
Expand Down
4 changes: 0 additions & 4 deletions ios/RCTMarkdownUtils.mm
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,6 @@ - (NSAttributedString *)parseMarkdown:(nullable NSAttributedString *)input withA
NSRange rangeForBackground = [inputString characterAtIndex:range.location] == '\n' ? NSMakeRange(range.location + 1, range.length - 1) : range;
[attributedString addAttribute:NSBackgroundColorAttributeName value:_markdownStyle.preBackgroundColor range:rangeForBackground];
// TODO: pass background color and ranges to layout manager
} else if (type == "h1") {
NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
NSRange rangeWithHashAndSpace = NSMakeRange(range.location - 2, range.length + 2); // we also need to include prepending "# "
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:rangeWithHashAndSpace];
}
}

Expand Down
9 changes: 8 additions & 1 deletion ios/RCTTextInputComponentView+Markdown.mm
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,14 @@ - (BOOL)markdown__textOf:(NSAttributedString *)newText equals:(NSAttributedStrin
{
RCTMarkdownUtils *markdownUtils = [self getMarkdownUtils];
if (markdownUtils != nil) {
return [newText isEqualToAttributedString:oldText];
// Emoji characters are automatically assigned an AppleColorEmoji NSFont and the original font is moved to NSOriginalFont
// We need to remove these attributes before comparison
NSMutableAttributedString *newTextCopy = [newText mutableCopy];
NSMutableAttributedString *oldTextCopy = [oldText mutableCopy];
[newTextCopy removeAttribute:@"NSFont" range:NSMakeRange(0, newTextCopy.length)];
[oldTextCopy removeAttribute:@"NSFont" range:NSMakeRange(0, oldTextCopy.length)];
[oldTextCopy removeAttribute:@"NSOriginalFont" range:NSMakeRange(0, oldTextCopy.length)];
return [newTextCopy isEqualToAttributedString:oldTextCopy];
}

return [self markdown__textOf:newText equals:oldText];
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@expensify/react-native-live-markdown",
"version": "0.1.103",
"version": "0.1.114",
"description": "Drop-in replacement for React Native's TextInput component with Markdown formatting.",
"main": "lib/commonjs/index",
"module": "lib/module/index",
Expand Down
99 changes: 99 additions & 0 deletions parser/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,13 @@ test('labeled link', () => {
{type: 'link', start: 7, length: 19},
{type: 'syntax', start: 26, length: 1},
]);

expect('[ Link ](https://example.com)').toBeParsedAs([
{type: 'syntax', start: 0, length: 1},
{type: 'syntax', start: 7, length: 2},
{type: 'link', start: 9, length: 19},
{type: 'syntax', start: 28, length: 1},
]);
});

test('link with same label as href', () => {
Expand Down Expand Up @@ -177,6 +184,15 @@ describe('email with same label as address', () => {
});
});

test('email with multiline hyperlinks', () => {
expect('[test\ntest](test@test.com)').toBeParsedAs([
{type: 'syntax', start: 0, length: 1},
{type: 'syntax', start: 10, length: 2},
{type: 'link', start: 12, length: 13},
{type: 'syntax', start: 25, length: 1},
]);
});

test('inline code', () => {
expect('Hello `world`!').toBeParsedAs([
{type: 'syntax', start: 6, length: 1},
Expand Down Expand Up @@ -541,3 +557,86 @@ describe('report mentions', () => {
expect('reported #report-name!').toBeParsedAs([{type: 'mention-report', start: 9, length: 12}]);
});
});

describe('inline video', () => {
test('with alt text', () => {
expect('![test](https://example.com/video.mp4)').toBeParsedAs([
{type: 'syntax', start: 0, length: 1},
{type: 'syntax', start: 1, length: 1},
{type: 'syntax', start: 6, length: 1},
{type: 'syntax', start: 7, length: 1},
{type: 'link', start: 8, length: 29},
{type: 'syntax', start: 37, length: 1},
]);
});

test('without alt text', () => {
expect('![](https://example.com/video.mp4)').toBeParsedAs([
{type: 'syntax', start: 0, length: 1},
{type: 'syntax', start: 1, length: 1},
{type: 'syntax', start: 2, length: 1},
{type: 'syntax', start: 3, length: 1},
{type: 'link', start: 4, length: 29},
{type: 'syntax', start: 33, length: 1},
]);
});

test('with same alt text as src', () => {
expect('![https://example.com/video.mp4](https://example.com/video.mp4)').toBeParsedAs([
{type: 'syntax', start: 0, length: 1},
{type: 'syntax', start: 1, length: 1},
{type: 'syntax', start: 31, length: 1},
{type: 'syntax', start: 32, length: 1},
{type: 'link', start: 33, length: 29},
{type: 'syntax', start: 62, length: 1},
]);
});

test('with alt text containing markdown', () => {
expect('![# fake-heading *bold* _italic_ ~strike~ [:-)]](https://example.com/video.mp4)').toBeParsedAs([
{type: 'syntax', start: 0, length: 1},
{type: 'syntax', start: 1, length: 1},
{type: 'syntax', start: 47, length: 1},
{type: 'syntax', start: 48, length: 1},
{type: 'link', start: 49, length: 29},
{type: 'syntax', start: 78, length: 1},
]);
});

test('trying to pass additional attributes', () => {
expect('![test](https://example.com/video.mp4 "title" class="video")').toBeParsedAs([{type: 'link', start: 8, length: 29}]);
});

test('trying to inject additional attributes', () => {
expect('![test" onerror="alert(\'xss\')](https://example.com/video.mp4)').toBeParsedAs([
{type: 'syntax', start: 0, length: 1},
{type: 'syntax', start: 1, length: 1},
{type: 'syntax', start: 29, length: 1},
{type: 'syntax', start: 30, length: 1},
{type: 'link', start: 31, length: 29},
{type: 'syntax', start: 60, length: 1},
]);
});

test('inline code in alt', () => {
expect('![`code`](https://example.com/video.mp4)').toBeParsedAs([
{type: 'syntax', start: 0, length: 1},
{type: 'syntax', start: 1, length: 1},
{type: 'syntax', start: 8, length: 1},
{type: 'syntax', start: 9, length: 1},
{type: 'link', start: 10, length: 29},
{type: 'syntax', start: 39, length: 1},
]);
});

test('blockquote in alt', () => {
expect('![```test```](https://example.com/video.mp4)').toBeParsedAs([
{type: 'syntax', start: 0, length: 1},
{type: 'syntax', start: 1, length: 1},
{type: 'syntax', start: 12, length: 1},
{type: 'syntax', start: 13, length: 1},
{type: 'link', start: 14, length: 29},
{type: 'syntax', start: 43, length: 1},
]);
});
});
17 changes: 16 additions & 1 deletion parser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ function parseTreeToTextAndRanges(tree: StackItem): [string, Range[]] {
} else if (node.tag === '<h1>') {
appendSyntax('# ');
addChildrenWithStyle(node, 'h1');
} else if (node.tag === '<br />') {
text += '\n';
} else if (node.tag.startsWith('<pre')) {
appendSyntax('```');
const content = node.children.join('').replaceAll('&#32;', ' ');
Expand Down Expand Up @@ -189,6 +191,20 @@ function parseTreeToTextAndRanges(tree: StackItem): [string, Range[]] {
appendSyntax('(');
addChildrenWithStyle(linkString, 'link');
appendSyntax(')');
} else if (node.tag.startsWith('<video data-expensify-source="')) {
const src = node.tag.match(/data-expensify-source="([^"]*)"/)![1]!; // always present
const rawLink = node.tag.match(/data-raw-href="([^"]*)"/);
const hasAlt = node.tag.match(/data-link-variant="([^"]*)"/)![1] === 'labeled';
const linkString = rawLink ? unescapeText(rawLink[1]!) : src;
appendSyntax('!');
if (hasAlt) {
appendSyntax('[');
node.children.forEach((child) => processChildren(child));
appendSyntax(']');
}
appendSyntax('(');
addChildrenWithStyle(linkString, 'link');
appendSyntax(')');
} else {
throw new Error(`[react-native-live-markdown] Error in function parseTreeToTextAndRanges: Unknown tag '${node.tag}'. This tag is not supported in this function's logic.`);
}
Expand Down Expand Up @@ -254,7 +270,6 @@ function parseExpensiMarkToRanges(markdown: string): Range[] {
const groupedRanges = groupRanges(sortedRanges);
return groupedRanges;
} catch (error) {
console.error(error);
// returning an empty array in case of error
return [];
}
Expand Down
2 changes: 1 addition & 1 deletion parser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@
"typescript": "^5.3.3"
},
"dependencies": {
"expensify-common": "2.0.35"
"expensify-common": "2.0.72"
}
}
41 changes: 21 additions & 20 deletions parser/react-native-live-markdown-parser.js

Large diffs are not rendered by default.

16 changes: 13 additions & 3 deletions src/MarkdownTextInput.web.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import React, {useEffect, useRef, useCallback, useMemo, useLayoutEffect} from 'r
import type {CSSProperties, MutableRefObject, ReactEventHandler, FocusEventHandler, MouseEvent, KeyboardEvent, SyntheticEvent, ClipboardEventHandler} from 'react';
import {StyleSheet} from 'react-native';
import {updateInputStructure} from './web/utils/parserUtils';
import BrowserUtils from './web/utils/browserUtils';
import InputHistory from './web/InputHistory';
import type {TreeNode} from './web/utils/treeUtils';
import {getCurrentCursorPosition, removeSelection, setCursorPosition} from './web/utils/cursorUtils';
Expand Down Expand Up @@ -97,6 +96,7 @@ const MarkdownTextInput = React.forwardRef<TextInput, MarkdownTextInputProps>(
autoFocus = false,
onContentSizeChange,
id,
inputMode,
},
ref,
) => {
Expand Down Expand Up @@ -295,7 +295,7 @@ const MarkdownTextInput = React.forwardRef<TextInput, MarkdownTextInputProps>(
const prevSelection = contentSelection.current ?? {start: 0, end: 0};
const newCursorPosition = Math.max(Math.max(contentSelection.current.end, 0) + (parsedText.length - previousText.length), 0);

if (compositionRef.current && !BrowserUtils.isMobile) {
if (compositionRef.current) {
divRef.current.value = parsedText;
compositionRef.current = false;
contentSelection.current.end = newCursorPosition;
Expand Down Expand Up @@ -346,7 +346,7 @@ const MarkdownTextInput = React.forwardRef<TextInput, MarkdownTextInputProps>(
if (inputType === 'deleteContentBackward') {
// When the user does a backspace delete he expects the content before the cursor to be removed.
// For this the start value needs to be adjusted (its as if the selection was before the text that we want to delete)
start -= before;
start = Math.max(start - before, 0);
}

event.nativeEvent.count = count;
Expand Down Expand Up @@ -552,6 +552,14 @@ const MarkdownTextInput = React.forwardRef<TextInput, MarkdownTextInputProps>(
compositionRef.current = true;
}, []);

const endComposition = useCallback(
(e) => {
compositionRef.current = false;
handleOnChangeText(e);
},
[handleOnChangeText],
);

const setRef = (currentRef: HTMLDivElement | null) => {
const r = currentRef;
if (r) {
Expand Down Expand Up @@ -651,6 +659,7 @@ const MarkdownTextInput = React.forwardRef<TextInput, MarkdownTextInputProps>(
className={className}
onKeyDown={handleKeyPress}
onCompositionStart={startComposition}
onCompositionEnd={endComposition}
onKeyUp={updateSelection}
onInput={handleOnChangeText}
onClick={handleClick}
Expand All @@ -662,6 +671,7 @@ const MarkdownTextInput = React.forwardRef<TextInput, MarkdownTextInputProps>(
placeholder={heightSafePlaceholder}
spellCheck={spellCheck}
dir={dir}
inputMode={inputMode}
/>
);
},
Expand Down
Loading
Loading
You are viewing a condensed version of this merge commit. You can view the full changes here.