Skip to content

Commit

Permalink
fix: disable static asset mangling for v2 Content Libraries
Browse files Browse the repository at this point in the history
The static asset substitution used to make images show up properly when
in the TinyMCE editor doesn't work for Content Libraries. Unfortunately,
this will cause the static asset references in XBlock content to get
mangled and saved incorrectly. So until we can handle it correctly,
we're just going to disable it entirely if the LearningContext is a v2
Content Library.

This means that static assets won't display properly in the editor
itself, but it should at least get written/preserved correctly, so that
those assets will show up properly in XBlock previews.
  • Loading branch information
ormsbee committed Oct 16, 2024
1 parent 7c5c518 commit 234496b
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/editors/sharedComponents/TinyMceWidget/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import pluginConfig from './pluginConfig';
import * as module from './hooks';
import * as tinyMCE from '../../data/constants/tinyMCE';
import { getRelativeUrl, getStaticUrl, parseAssetName } from './utils';
import { isLibraryKey } from '../../../generic/key-utils';

export const state = StrictDict({
// eslint-disable-next-line react-hooks/rules-of-hooks
Expand Down Expand Up @@ -83,6 +84,20 @@ 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 @@ -98,7 +113,8 @@ export const replaceStaticWithAsset = ({
const assetName = parseAssetName(src);
const displayName = isStatic ? staticName : assetName;
const isCorrectAssetFormat = assetSrc.startsWith('/asset') && 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

// 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 (isCorrectAssetFormat) {
Expand Down

0 comments on commit 234496b

Please sign in to comment.