From a6bdc272d4c9a04cdaa177d2f8b1ccfc5abc9fca Mon Sep 17 00:00:00 2001 From: KristinAoki Date: Fri, 28 Jun 2024 11:49:44 -0700 Subject: [PATCH 1/3] fix: pasting and images only insert at beginning --- .../sharedComponents/TinyMceWidget/hooks.js | 62 ++++++++++--------- 1 file changed, 32 insertions(+), 30 deletions(-) diff --git a/src/editors/sharedComponents/TinyMceWidget/hooks.js b/src/editors/sharedComponents/TinyMceWidget/hooks.js index 22e814d43..db67e3109 100644 --- a/src/editors/sharedComponents/TinyMceWidget/hooks.js +++ b/src/editors/sharedComponents/TinyMceWidget/hooks.js @@ -77,38 +77,40 @@ export const replaceStaticWithAsset = ({ lmsEndpointUrl, }) => { let content = initialContent; + let hasChanges = false; const srcs = content.split(/(src="|src="|href="|href=")/g).filter( src => src.startsWith('/static') || src.startsWith('/asset'), ); - if (isEmpty(srcs)) { - return initialContent; - } - srcs.forEach(src => { - const currentContent = content; - let staticFullUrl; - const isStatic = src.startsWith('/static/'); - const assetSrc = src.substring(0, src.indexOf('"')); - const staticName = assetSrc.substring(8); - const assetName = assetSrc.replace(/\/assets\/.+[^/]\//g, ''); - const displayName = isStatic ? staticName : assetName; - const isCorrectAssetFormat = assetSrc.match(/\/asset-v1:\S+[+]\S+[@]\S+[+]\S+[@]/g)?.length >= 1; - // assets in expandable text areas so not support relative urls so all assets must have the lms - // endpoint prepended to the relative url - if (editorType === 'expandable') { - if (isCorrectAssetFormat) { - staticFullUrl = `${lmsEndpointUrl}${assetSrc}`; - } else { - staticFullUrl = `${lmsEndpointUrl}${getRelativeUrl({ courseId: learningContextId, displayName })}`; + if (!isEmpty(srcs)) { + srcs.forEach(src => { + const currentContent = content; + let staticFullUrl; + const isStatic = src.startsWith('/static/'); + const assetSrc = src.substring(0, src.indexOf('"')); + const staticName = assetSrc.substring(8); + const assetName = assetSrc.replace(/\/assets\/.+[^/]\//g, ''); + const displayName = isStatic ? staticName : assetName; + const isCorrectAssetFormat = assetSrc.match(/\/asset-v1:\S+[+]\S+[@]\S+[+]\S+[@]/g)?.length >= 1; + // assets in expandable text areas so not support relative urls so all assets must have the lms + // endpoint prepended to the relative url + if (editorType === 'expandable') { + if (isCorrectAssetFormat) { + staticFullUrl = `${lmsEndpointUrl}${assetSrc}`; + } else { + staticFullUrl = `${lmsEndpointUrl}${getRelativeUrl({ courseId: learningContextId, displayName })}`; + } + } else if (!isCorrectAssetFormat) { + staticFullUrl = getRelativeUrl({ courseId: learningContextId, displayName }); } - } else if (!isCorrectAssetFormat) { - staticFullUrl = getRelativeUrl({ courseId: learningContextId, displayName }); - } - if (staticFullUrl) { - const currentSrc = src.substring(0, src.indexOf('"')); - content = currentContent.replace(currentSrc, staticFullUrl); - } - }); - return content; + if (staticFullUrl) { + const currentSrc = src.substring(0, src.indexOf('"')); + content = currentContent.replace(currentSrc, staticFullUrl); + hasChanges = true; + } + }); + if (hasChanges) { return content; } + } + return false; }; export const getImageResizeHandler = ({ editor, imagesRef, setImage }) => () => { @@ -196,7 +198,7 @@ export const setupCustomBehavior = ({ lmsEndpointUrl, learningContextId, }); - updateContent(newContent); + if (newContent) { updateContent(newContent); } }); } editor.on('ExecCommand', (e) => { @@ -206,7 +208,7 @@ export const setupCustomBehavior = ({ initialContent, learningContextId, }); - editor.setContent(newContent); + if (newContent) { editor.setContent(newContent); } } if (e.command === 'RemoveFormat') { editor.formatter.remove('blockquote'); From 5448482556a998565699ab61524e5deec8782656 Mon Sep 17 00:00:00 2001 From: KristinAoki Date: Fri, 28 Jun 2024 12:01:54 -0700 Subject: [PATCH 2/3] fix: add image click not showing gallery --- src/editors/sharedComponents/ImageUploadModal/index.jsx | 4 +++- src/editors/sharedComponents/ImageUploadModal/index.test.jsx | 1 + 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/editors/sharedComponents/ImageUploadModal/index.jsx b/src/editors/sharedComponents/ImageUploadModal/index.jsx index df2112ec4..80af029a7 100644 --- a/src/editors/sharedComponents/ImageUploadModal/index.jsx +++ b/src/editors/sharedComponents/ImageUploadModal/index.jsx @@ -87,6 +87,7 @@ export const hooks = { updateReactState({ settings, ...args }); close(); + args.setSelection(null); }, onClose: ({ clearSelection, close }) => () => { clearSelection(); @@ -130,7 +131,7 @@ export const ImageUploadModal = ({ { expect(updateImageDimensionsSpy.mock.results[0].value.foundMatch).toBe(false); expect(images.current).toEqual([mockImage, newImage]); expect(close).toBeCalled(); + expect(setSelection).toBeCalledWith(null); }, ); }); From 6b127260d5825e13aaa82b7f12a69920c222cff9 Mon Sep 17 00:00:00 2001 From: KristinAoki Date: Fri, 28 Jun 2024 12:38:23 -0700 Subject: [PATCH 3/3] chore: increase code coverage --- .../TinyMceWidget/hooks.test.js | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/editors/sharedComponents/TinyMceWidget/hooks.test.js b/src/editors/sharedComponents/TinyMceWidget/hooks.test.js index 67096eb91..525925c1f 100644 --- a/src/editors/sharedComponents/TinyMceWidget/hooks.test.js +++ b/src/editors/sharedComponents/TinyMceWidget/hooks.test.js @@ -182,17 +182,17 @@ describe('TinyMceEditor hooks', () => { }); describe('replaceStaticWithAsset', () => { - const initialContent = 'test'; - const learningContextId = 'course+test+run'; + const initialContent = 'test'; + const learningContextId = 'course-v1:org+test+run'; const lmsEndpointUrl = 'sOmEvaLue.cOm'; - it('it returns updated src for text editor to update content', () => { - const expected = 'test'; + it('returns updated src for text editor to update content', () => { + const expected = 'test'; const actual = module.replaceStaticWithAsset({ initialContent, learningContextId }); expect(actual).toEqual(expected); }); - it('it returs updated src with absolute url for expandable editor to update content', () => { + it('returns updated src with absolute url for expandable editor to update content', () => { const editorType = 'expandable'; - const expected = `test`; + const expected = `test`; const actual = module.replaceStaticWithAsset({ initialContent, editorType, @@ -201,6 +201,11 @@ describe('TinyMceEditor hooks', () => { }); expect(actual).toEqual(expected); }); + it('returns false when there are no srcs to update', () => { + const content = '
Hello world!
'; + const actual = module.replaceStaticWithAsset({ initialContent: content, learningContextId }); + expect(actual).toBeFalsy(); + }); }); describe('setAssetToStaticUrl', () => { it('returns content with updated img links', () => {