Skip to content

Commit fc3cd9a

Browse files
authored
fix: image and paste insert (#490)
* fix: pasting and images only insert at beginning * fix: add image click not showing gallery * chore: increase code coverage
1 parent 2678234 commit fc3cd9a

File tree

4 files changed

+47
-37
lines changed

4 files changed

+47
-37
lines changed

src/editors/sharedComponents/ImageUploadModal/index.jsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ export const hooks = {
8787
updateReactState({ settings, ...args });
8888

8989
close();
90+
args.setSelection(null);
9091
},
9192
onClose: ({ clearSelection, close }) => () => {
9293
clearSelection();
@@ -130,7 +131,7 @@ export const ImageUploadModal = ({
130131
<ImageSettingsModal
131132
{...{
132133
isOpen,
133-
close: module.hooks.onClose({ editorRef, clearSelection, close }),
134+
close: module.hooks.onClose({ clearSelection, close }),
134135
selection,
135136
images,
136137
saveToEditor: module.hooks.createSaveCallback({
@@ -141,6 +142,7 @@ export const ImageUploadModal = ({
141142
selection,
142143
setSelection,
143144
lmsEndpointUrl,
145+
clearSelection,
144146
}),
145147
returnToSelection: clearSelection,
146148
}}

src/editors/sharedComponents/ImageUploadModal/index.test.jsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ describe('ImageUploadModal', () => {
128128
expect(updateImageDimensionsSpy.mock.results[0].value.foundMatch).toBe(false);
129129
expect(images.current).toEqual([mockImage, newImage]);
130130
expect(close).toBeCalled();
131+
expect(setSelection).toBeCalledWith(null);
131132
},
132133
);
133134
});

src/editors/sharedComponents/TinyMceWidget/hooks.js

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -77,38 +77,40 @@ export const replaceStaticWithAsset = ({
7777
lmsEndpointUrl,
7878
}) => {
7979
let content = initialContent;
80+
let hasChanges = false;
8081
const srcs = content.split(/(src="|src=&quot;|href="|href=&quot)/g).filter(
8182
src => src.startsWith('/static') || src.startsWith('/asset'),
8283
);
83-
if (isEmpty(srcs)) {
84-
return initialContent;
85-
}
86-
srcs.forEach(src => {
87-
const currentContent = content;
88-
let staticFullUrl;
89-
const isStatic = src.startsWith('/static/');
90-
const assetSrc = src.substring(0, src.indexOf('"'));
91-
const staticName = assetSrc.substring(8);
92-
const assetName = assetSrc.replace(/\/assets\/.+[^/]\//g, '');
93-
const displayName = isStatic ? staticName : assetName;
94-
const isCorrectAssetFormat = assetSrc.match(/\/asset-v1:\S+[+]\S+[@]\S+[+]\S+[@]/g)?.length >= 1;
95-
// assets in expandable text areas so not support relative urls so all assets must have the lms
96-
// endpoint prepended to the relative url
97-
if (editorType === 'expandable') {
98-
if (isCorrectAssetFormat) {
99-
staticFullUrl = `${lmsEndpointUrl}${assetSrc}`;
100-
} else {
101-
staticFullUrl = `${lmsEndpointUrl}${getRelativeUrl({ courseId: learningContextId, displayName })}`;
84+
if (!isEmpty(srcs)) {
85+
srcs.forEach(src => {
86+
const currentContent = content;
87+
let staticFullUrl;
88+
const isStatic = src.startsWith('/static/');
89+
const assetSrc = src.substring(0, src.indexOf('"'));
90+
const staticName = assetSrc.substring(8);
91+
const assetName = assetSrc.replace(/\/assets\/.+[^/]\//g, '');
92+
const displayName = isStatic ? staticName : assetName;
93+
const isCorrectAssetFormat = assetSrc.match(/\/asset-v1:\S+[+]\S+[@]\S+[+]\S+[@]/g)?.length >= 1;
94+
// assets in expandable text areas so not support relative urls so all assets must have the lms
95+
// endpoint prepended to the relative url
96+
if (editorType === 'expandable') {
97+
if (isCorrectAssetFormat) {
98+
staticFullUrl = `${lmsEndpointUrl}${assetSrc}`;
99+
} else {
100+
staticFullUrl = `${lmsEndpointUrl}${getRelativeUrl({ courseId: learningContextId, displayName })}`;
101+
}
102+
} else if (!isCorrectAssetFormat) {
103+
staticFullUrl = getRelativeUrl({ courseId: learningContextId, displayName });
102104
}
103-
} else if (!isCorrectAssetFormat) {
104-
staticFullUrl = getRelativeUrl({ courseId: learningContextId, displayName });
105-
}
106-
if (staticFullUrl) {
107-
const currentSrc = src.substring(0, src.indexOf('"'));
108-
content = currentContent.replace(currentSrc, staticFullUrl);
109-
}
110-
});
111-
return content;
105+
if (staticFullUrl) {
106+
const currentSrc = src.substring(0, src.indexOf('"'));
107+
content = currentContent.replace(currentSrc, staticFullUrl);
108+
hasChanges = true;
109+
}
110+
});
111+
if (hasChanges) { return content; }
112+
}
113+
return false;
112114
};
113115

114116
export const getImageResizeHandler = ({ editor, imagesRef, setImage }) => () => {
@@ -196,7 +198,7 @@ export const setupCustomBehavior = ({
196198
lmsEndpointUrl,
197199
learningContextId,
198200
});
199-
updateContent(newContent);
201+
if (newContent) { updateContent(newContent); }
200202
});
201203
}
202204
editor.on('ExecCommand', (e) => {
@@ -206,7 +208,7 @@ export const setupCustomBehavior = ({
206208
initialContent,
207209
learningContextId,
208210
});
209-
editor.setContent(newContent);
211+
if (newContent) { editor.setContent(newContent); }
210212
}
211213
if (e.command === 'RemoveFormat') {
212214
editor.formatter.remove('blockquote');

src/editors/sharedComponents/TinyMceWidget/hooks.test.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -182,17 +182,17 @@ describe('TinyMceEditor hooks', () => {
182182
});
183183

184184
describe('replaceStaticWithAsset', () => {
185-
const initialContent = '<img src="/static/soMEImagEURl1.jpeg"/><a href="/assets/v1/some-key/test.pdf">test</a>';
186-
const learningContextId = 'course+test+run';
185+
const initialContent = '<img src="/static/soMEImagEURl1.jpeg"/><a href="/assets/v1/some-key/test.pdf">test</a><img src="/asset-v1:org+test+run+type@[email protected]" />';
186+
const learningContextId = 'course-v1:org+test+run';
187187
const lmsEndpointUrl = 'sOmEvaLue.cOm';
188-
it('it returns updated src for text editor to update content', () => {
189-
const expected = '<img src="/asset+test+run+type@[email protected]"/><a href="/asset+test+run+type@[email protected]">test</a>';
188+
it('returns updated src for text editor to update content', () => {
189+
const expected = '<img src="/asset-v1:org+test+run+type@[email protected]"/><a href="/asset-v1:org+test+run+type@[email protected]">test</a><img src="/asset-v1:org+test+run+type@[email protected]" />';
190190
const actual = module.replaceStaticWithAsset({ initialContent, learningContextId });
191191
expect(actual).toEqual(expected);
192192
});
193-
it('it returs updated src with absolute url for expandable editor to update content', () => {
193+
it('returns updated src with absolute url for expandable editor to update content', () => {
194194
const editorType = 'expandable';
195-
const expected = `<img src="${lmsEndpointUrl}/asset+test+run+type@[email protected]"/><a href="${lmsEndpointUrl}/asset+test+run+type@[email protected]">test</a>`;
195+
const expected = `<img src="${lmsEndpointUrl}/asset-v1:org+test+run+type@[email protected]"/><a href="${lmsEndpointUrl}/asset-v1:org+test+run+type@[email protected]">test</a><img src="${lmsEndpointUrl}/asset-v1:org+test+run+type@[email protected]" />`;
196196
const actual = module.replaceStaticWithAsset({
197197
initialContent,
198198
editorType,
@@ -201,6 +201,11 @@ describe('TinyMceEditor hooks', () => {
201201
});
202202
expect(actual).toEqual(expected);
203203
});
204+
it('returns false when there are no srcs to update', () => {
205+
const content = '<div>Hello world!</div>';
206+
const actual = module.replaceStaticWithAsset({ initialContent: content, learningContextId });
207+
expect(actual).toBeFalsy();
208+
});
204209
});
205210
describe('setAssetToStaticUrl', () => {
206211
it('returns content with updated img links', () => {

0 commit comments

Comments
 (0)