Skip to content

Commit

Permalink
Merge pull request #42463 from dominictb/fix/41952
Browse files Browse the repository at this point in the history
fix: cache extra attributes for video markdown conversion
  • Loading branch information
tgolen authored Jun 19, 2024
2 parents a66ed89 + a2cfd54 commit 768da63
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 17 deletions.
11 changes: 5 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@
"date-fns-tz": "^2.0.0",
"dom-serializer": "^0.2.2",
"domhandler": "^4.3.0",
"expensify-common": "^2.0.12",
"expensify-common": "2.0.17",
"expo": "^50.0.3",
"expo-av": "~13.10.4",
"expo-image": "1.11.0",
Expand Down
18 changes: 14 additions & 4 deletions src/libs/OnyxAwareParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,22 @@ Onyx.connect({
},
});

function parseHtmlToMarkdown(html: string, reportIDToName?: Record<string, string>, accountIDToName?: Record<string, string>): string {
return parser.htmlToMarkdown(html, {reportIDToName: reportIDToName ?? reportIDToNameMap, accountIDToName: accountIDToName ?? accountIDToNameMap});
function parseHtmlToMarkdown(
html: string,
reportIDToName?: Record<string, string>,
accountIDToName?: Record<string, string>,
cacheVideoAttributes?: (videoSource: string, videoAttrs: string) => void,
): string {
return parser.htmlToMarkdown(html, {reportIDToName: reportIDToName ?? reportIDToNameMap, accountIDToName: accountIDToName ?? accountIDToNameMap, cacheVideoAttributes});
}

function parseHtmlToText(html: string, reportIDToName?: Record<string, string>, accountIDToName?: Record<string, string>): string {
return parser.htmlToText(html, {reportIDToName: reportIDToName ?? reportIDToNameMap, accountIDToName: accountIDToName ?? accountIDToNameMap});
function parseHtmlToText(
html: string,
reportIDToName?: Record<string, string>,
accountIDToName?: Record<string, string>,
cacheVideoAttributes?: (videoSource: string, videoAttrs: string) => void,
): string {
return parser.htmlToText(html, {reportIDToName: reportIDToName ?? reportIDToNameMap, accountIDToName: accountIDToName ?? accountIDToNameMap, cacheVideoAttributes});
}

export {parseHtmlToMarkdown, parseHtmlToText};
12 changes: 8 additions & 4 deletions src/libs/actions/Report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1452,19 +1452,23 @@ function removeLinksFromHtml(html: string, links: string[]): string {
*
* @param newCommentText text of the comment after editing.
* @param originalCommentMarkdown original markdown of the comment before editing.
* @param videoAttributeCache cache of video attributes ([videoSource]: videoAttributes)
*/
function handleUserDeletedLinksInHtml(newCommentText: string, originalCommentMarkdown: string): string {
function handleUserDeletedLinksInHtml(newCommentText: string, originalCommentMarkdown: string, videoAttributeCache?: Record<string, string>): string {
const parser = new ExpensiMark();
if (newCommentText.length > CONST.MAX_MARKUP_LENGTH) {
return newCommentText;
}
const htmlForNewComment = parser.replace(newCommentText);

const htmlForNewComment = parser.replace(newCommentText, {
extras: {videoAttributeCache},
});
const removedLinks = parser.getRemovedMarkdownLinks(originalCommentMarkdown, newCommentText);
return removeLinksFromHtml(htmlForNewComment, removedLinks);
}

/** Saves a new message for a comment. Marks the comment as edited, which will be reflected in the UI. */
function editReportComment(reportID: string, originalReportAction: OnyxEntry<ReportAction>, textForNewComment: string) {
function editReportComment(reportID: string, originalReportAction: OnyxEntry<ReportAction>, textForNewComment: string, videoAttributeCache?: Record<string, string>) {
const parser = new ExpensiMark();
const originalReportID = ReportUtils.getOriginalReportID(reportID, originalReportAction);

Expand All @@ -1482,8 +1486,8 @@ function editReportComment(reportID: string, originalReportAction: OnyxEntry<Rep
if (originalCommentMarkdown === textForNewComment) {
return;
}
const htmlForNewComment = handleUserDeletedLinksInHtml(textForNewComment, originalCommentMarkdown, videoAttributeCache);

const htmlForNewComment = handleUserDeletedLinksInHtml(textForNewComment, originalCommentMarkdown);
const reportComment = parseHtmlToText(htmlForNewComment);

// For comments shorter than or equal to 10k chars, convert the comment from MD into HTML because that's how it is stored in the database
Expand Down
11 changes: 9 additions & 2 deletions src/pages/home/report/ReportActionItemMessageEdit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ const messageEditInput = 'messageEditInput';

const shouldUseForcedSelectionRange = shouldUseEmojiPickerSelection();

// video source -> video attributes
const draftMessageVideoAttributeCache = new Map<string, string>();

function ReportActionItemMessageEdit(
{action, draftMessage, reportID, index, shouldDisableEmojiPicker = false}: ReportActionItemMessageEditProps,
forwardedRef: ForwardedRef<TextInput | HTMLTextAreaElement | undefined>,
Expand Down Expand Up @@ -105,7 +108,11 @@ function ReportActionItemMessageEdit(
const isCommentPendingSaved = useRef(false);

useEffect(() => {
const originalMessage = parseHtmlToMarkdown(action.message?.[0]?.html ?? '');
draftMessageVideoAttributeCache.clear();

const originalMessage = parseHtmlToMarkdown(action.message?.[0]?.html ?? '', undefined, undefined, (videoSource, attrs) => {
draftMessageVideoAttributeCache.set(videoSource, attrs);
});
if (ReportActionsUtils.isDeletedAction(action) || !!(action.message && draftMessage === originalMessage) || !!(prevDraftMessage === draftMessage || isCommentPendingSaved.current)) {
return;
}
Expand Down Expand Up @@ -296,7 +303,7 @@ function ReportActionItemMessageEdit(
ReportActionContextMenu.showDeleteModal(reportID, action, true, deleteDraft, () => focusEditAfterCancelDelete(textInputRef.current));
return;
}
Report.editReportComment(reportID, action, trimmedNewDraft);
Report.editReportComment(reportID, action, trimmedNewDraft, Object.fromEntries(draftMessageVideoAttributeCache));
deleteDraft();
}, [action, deleteDraft, draft, reportID]);

Expand Down

0 comments on commit 768da63

Please sign in to comment.