Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(edit-content): Make our dotAddImage custom plugins for TinyMCE work in Angular FILEASSET #27969 #28075

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ describe('formatDotImageNode', () => {
it('should return formatted image node', () => {
const asset: DotCMSContentlet = {
...EMPTY_CONTENTLET,
baseType: 'DOTASSET',
assetVersion: 'version',
asset: 'asset',
title: 'title',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { DotCMSContentlet } from '@dotcms/dotcms-models';
import { getImageAssetUrl } from '@dotcms/utils';

export const formatDotImageNode = (asset: DotCMSContentlet) => {
return `<img src="${asset.assetVersion || asset.asset}"
return `<img src="${getImageAssetUrl(asset)}"
alt="${asset.title}"
data-field-name="${asset.titleImage}"
data-inode="${asset.inode}"
Expand Down
109 changes: 69 additions & 40 deletions core-web/libs/utils/src/lib/dot-utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,69 @@
// import { DotPageRenderState } from '../../../../apps/dotcms-ui/src/app/portlets/dot-edit-page/shared/models';
// import { mockDotRenderedPage } from '../../../../apps/dotcms-ui/src/app/test/dot-page-render.mock';
// import { mockUser } from '../../../../apps/dotcms-ui/src/app/test/login-service.mock';
// import { getDownloadLink } from './dot-utils';
// import { DotPageRender } from '@dotcms/dotcms-models';

// describe('Dot Utils', () => {
// it('should return anchor with the correct values', () => {
// const blobMock = new Blob(['']);
// const fileName = 'doc.txt';
// spyOn(window.URL, 'createObjectURL');
// const anchor = getDownloadLink(blobMock, fileName);

// expect(anchor.download).toEqual(fileName);
// expect(window.URL.createObjectURL).toHaveBeenCalledWith(blobMock);
// });

// it('should return unique URL with host, language and device Ids', () => {
// const mockRenderedPageState = new DotPageRenderState(
// mockUser(),
// new DotPageRender({
// ...mockDotRenderedPage(),
// viewAs: {
// ...mockDotRenderedPage().viewAs,
// device: {
// identifier: 'abc123',
// cssHeight: '800',
// cssWidth: '1200',
// name: 'custom',
// inode: '123zxc'
// }
// }
// })
// );

// const url = dotUtils.generateDotFavoritePageUrl(mockRenderedPageState);

// expect(url).toEqual('/an/url/test?&language_id=1&device_id=abc123');
// });
// });
import { DotCMSBaseTypesContentTypes } from '@dotcms/dotcms-models';
import { EMPTY_CONTENTLET } from '@dotcms/utils-testing';

import { getImageAssetUrl } from './dot-utils';

describe('Dot Utils', () => {
describe('getImageAssetUrl', () => {
it('should return fileAssetVersion when baseType is FILEASSET and fileAssetVersion is defined', () => {
const contentlet = {
...EMPTY_CONTENTLET,
baseType: DotCMSBaseTypesContentTypes.FILEASSET,
fileAssetVersion: 'fileAssetVersion',
fileAsset: 'fileAsset'
};

expect(getImageAssetUrl(contentlet)).toEqual('fileAssetVersion');
});

it('should return fileAsset when baseType is FILEASSET and fileAssetVersion is not defined', () => {
const contentlet = {
...EMPTY_CONTENTLET,
baseType: DotCMSBaseTypesContentTypes.FILEASSET,
fileAsset: 'fileAsset'
};

expect(getImageAssetUrl(contentlet)).toEqual('fileAsset');
});

it('should return assetVersion when baseType is DOTASSET and assetVersion is defined', () => {
const contentlet = {
...EMPTY_CONTENTLET,
baseType: DotCMSBaseTypesContentTypes.DOTASSET,
assetVersion: 'assetVersion',
asset: 'asset'
};

expect(getImageAssetUrl(contentlet)).toEqual('assetVersion');
});

it('should return asset when baseType is DOTASSET and assetVersion is not defined', () => {
const contentlet = {
...EMPTY_CONTENTLET,
baseType: DotCMSBaseTypesContentTypes.DOTASSET,
asset: 'asset'
};

expect(getImageAssetUrl(contentlet)).toEqual('asset');
});

it('should return asset when baseType is not FILEASSET or DOTASSET', () => {
const contentlet = {
...EMPTY_CONTENTLET,
baseType: 'OTHER',
asset: 'asset'
};

expect(getImageAssetUrl(contentlet)).toEqual('asset');
});

it('should return empty string when asset is not defined and baseType is not FILEASSET or DOTASSET', () => {
const contentlet = {
...EMPTY_CONTENTLET,
baseType: 'OTHER'
};

expect(getImageAssetUrl(contentlet)).toEqual('');
});
});
});
30 changes: 29 additions & 1 deletion core-web/libs/utils/src/lib/dot-utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { DotPageToolUrlParams } from '@dotcms/dotcms-models';
import {
DotCMSBaseTypesContentTypes,
DotCMSContentlet,
DotPageToolUrlParams
} from '@dotcms/dotcms-models';

/**
* Generate an anchor element with a Blob file to eventually be click to force a download
Expand Down Expand Up @@ -66,3 +70,27 @@ export function getRunnableLink(url: string, currentPageUrlParams: DotPageToolUr

return url;
}

/**
* Get the image asset URL
*
* @export
* @param {DotCMSContentlet} asset
* @return {*} {string}
*/
export function getImageAssetUrl(contentlet: DotCMSContentlet): string {
if (!contentlet?.baseType) {
return contentlet.asset;
}

switch (contentlet?.baseType) {
case DotCMSBaseTypesContentTypes.FILEASSET:
return contentlet.fileAssetVersion || contentlet.fileAsset;

case DotCMSBaseTypesContentTypes.DOTASSET:
return contentlet.assetVersion || contentlet.asset;

default:
return contentlet?.asset || '';
}
}
Loading