Skip to content

Commit

Permalink
fix: allow to preview images
Browse files Browse the repository at this point in the history
  • Loading branch information
Ian2012 committed Oct 18, 2024
1 parent 4facf1c commit ca9c6f1
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 16 deletions.
6 changes: 6 additions & 0 deletions src/editors/containers/TextEditor/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ const TextEditor = ({
learningContextId,
});
const editorContent = newContent || initialContent;
let documentURL;
if (isLibrary) {
// TODO Get the library base URL in the format http://{STUDIO_HOST}/library_assets/{component_uuid}/
documentURL = 'http://studio.local.openedx.io:8001/library_assets/9b40a992-9d5f-4f22-9d8c-5ff18c3600dc/';
}

if (!refReady) { return null; }

Expand All @@ -65,6 +70,7 @@ const TextEditor = ({
images,
isLibrary,
learningContextId,
documentURL,
}}
/>
);
Expand Down
42 changes: 26 additions & 16 deletions src/editors/sharedComponents/TinyMceWidget/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,20 +84,6 @@ export const replaceStaticWithAsset = ({
editorType,
lmsEndpointUrl,
}) => {
if (isLibraryKey(learningContextId)) {
// This function doesn't currently know how to deal with Library assets.
// Libraries don't mangle the path into an asset key–it might be sufficient
// to remove the initial "/" in a "/static/images/foo.png" link, and then
// set the base URL to the correct ComponentVersion base. If we let this
// function try to deal with Library assets, it would convert them in such a
// way that it wouldn't convert back later on, and we'd end up storing the
// incorrect OLX and breaking the display from that point forward.
//
// So until we handle it better, just disable static asset URL substitutions
// when dealing with Library content.
return false;
}

let content = initialContent;
let hasChanges = false;
const srcs = content.split(/(src="|src="|href="|href=&quot)/g).filter(
Expand All @@ -116,7 +102,21 @@ export const replaceStaticWithAsset = ({

// assets in expandable text areas do not support relative urls so all assets must have the lms
// endpoint prepended to the relative url
if (editorType === 'expandable') {
if (isLibraryKey(learningContextId)) {
// This function doesn't currently know how to deal with Library assets.
// Libraries don't mangle the path into an asset key–it might be sufficient
// to remove the initial "/" in a "/static/images/foo.png" link, and then
// set the base URL to the correct ComponentVersion base. If we let this
// function try to deal with Library assets, it would convert them in such a
// way that it wouldn't convert back later on, and we'd end up storing the
// incorrect OLX and breaking the display from that point forward.
//
// So until we handle it better, just disable static asset URL substitutions
// when dealing with Library content.
if (isStatic) {
staticFullUrl = assetSrc.substring(1);
}
} else if (editorType === 'expandable') {
if (isCorrectAssetFormat) {
staticFullUrl = `${lmsEndpointUrl}${assetSrc}`;
} else {
Expand Down Expand Up @@ -261,9 +261,12 @@ export const editorConfig = ({
content,
minHeight,
learningContextId,
documentURL,
}) => {
const lmsEndpointUrl = getConfig().LMS_BASE_URL;
const studioEndpointUrl = getConfig().STUDIO_BASE_URL;

const baseURL = documentURL || lmsEndpointUrl;
const {
toolbar,
config,
Expand All @@ -290,7 +293,7 @@ export const editorConfig = ({
min_height: minHeight,
contextmenu: 'link table',
directionality: isLocaleRtl ? 'rtl' : 'ltr',
document_base_url: lmsEndpointUrl,
document_base_url: baseURL,
imagetools_cors_hosts: [removeProtocolFromUrl(lmsEndpointUrl), removeProtocolFromUrl(studioEndpointUrl)],
imagetools_toolbar: imageToolbar,
formats: { label: { inline: 'label' } },
Expand Down Expand Up @@ -436,6 +439,13 @@ export const setAssetToStaticUrl = ({ editorValue, lmsEndpointUrl }) => {
const updatedContent = content.replace(currentSrc, portableUrl);
content = updatedContent;
});

assetSrcs.filter(src => src.startsWith('static/')).forEach(src => {
const portableUrl = `/${ src}`;
const currentSrc = src.substring(0, src.search(/("|")/));
const updatedContent = content.replace(currentSrc, portableUrl);
content = updatedContent;
});
return content;
};

Expand Down
2 changes: 2 additions & 0 deletions src/editors/sharedComponents/TinyMceWidget/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ const TinyMceWidget = ({
images,
isLibrary,
onChange,
documentURL,
...editorConfig
}) => {
const { isImgOpen, openImgModal, closeImgModal } = hooks.imgModalToggle();
Expand Down Expand Up @@ -85,6 +86,7 @@ const TinyMceWidget = ({
learningContextId,
images: imagesRef,
editorContentHtml,
documentURL,
...imageSelection,
...editorConfig,
})
Expand Down

0 comments on commit ca9c6f1

Please sign in to comment.