Skip to content

Commit

Permalink
IBX-6315: Edit/Preview embedded items (#123)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasOsti authored Oct 19, 2023
1 parent d4b9f58 commit cbae649
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ class IbexaEmbedContentInlineCommand extends Command {
});
}

createEmbed(writer, { contentId, contentName }) {
return writer.createElement('embedInline', { contentId, contentName });
createEmbed(writer, { contentId, contentName, locationId, languageCodes }) {
return writer.createElement('embedInline', { contentId, contentName, locationId, languageCodes });
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,27 @@ import IbexaEmbedContentInlineCommand from './embed-inline-command';

import { findContent } from '../../services/content-service';

const renderPreview = (title) => {
const renderPreview = (title, contentId) => {
return `<svg class="ibexa-icon ibexa-icon--medium ibexa-icon--secondary">
<use xlink:href="${window.ibexa.helpers.icon.getIconPath('embed')}"></use>
</svg>
<span class="ibexa-embed-content__title">${title}</span>`;
<use xlink:href="${window.ibexa.helpers.icon.getIconPath('embed')}"></use>
</svg>
<span
class="ibexa-embed-content__title"
data-ibexa-update-content-id="${contentId}"
data-ibexa-update-source-data-path="Content.Name"
>
${title}
</span>
<span>
<button
type="button"
class="btn ibexa-btn ibexa-btn--small ibexa-btn--ghost ibexa-btn--no-text ibexa-embedded-item__actions-menu-trigger-btn"
>
<svg class="ibexa-icon ibexa-icon--small ibexa-icon--secondary">
<use xlink:href="${window.ibexa.helpers.icon.getIconPath('options')}"></use>
</svg>
</button>
</span>`;
};

class IbexaEmbedContentInlineEditing extends Plugin {
Expand All @@ -25,7 +41,7 @@ class IbexaEmbedContentInlineEditing extends Plugin {
isObject: true,
isInline: true,
allowWhere: '$text',
allowAttributes: ['contentId', 'contentName'],
allowAttributes: ['contentId', 'contentName', 'locationId', 'languageCodes'],
});
}

Expand Down Expand Up @@ -57,13 +73,35 @@ class IbexaEmbedContentInlineEditing extends Plugin {
})
.add((dispatcher) =>
dispatcher.on('attribute:contentName', (event, data, conversionApi) => {
const { editor } = this;
const downcastWriter = conversionApi.writer;
const modelElement = data.item;
const viewElement = conversionApi.mapper.toViewElement(modelElement);
const preview = downcastWriter.createUIElement('span', { class: 'ibexa-embed-content' }, function (domDocument) {
const contentId = modelElement.getAttribute('contentId');
const contentName = modelElement.getAttribute('contentName');
const locationId = modelElement.getAttribute('locationId');
const languageCodes = modelElement.getAttribute('languageCodes');
const domElement = this.toDomElement(domDocument);

domElement.innerHTML = renderPreview(modelElement.getAttribute('contentName'));
domElement.innerHTML = renderPreview(contentName, contentId);

const itemActionsTriggerElement = domElement.querySelector('.ibexa-embedded-item__actions-menu-trigger-btn');
const itemActionsMenuContainer = editor.sourceElement.parentNode.querySelector(
'.ibexa-embedded-item-actions .ibexa-multilevel-popup-menu',
);

domDocument.body.dispatchEvent(
new CustomEvent('ibexa-embedded-item:create-dynamic-menu', {
detail: {
contentId,
locationId,
languageCodes,
menuTriggerElement: itemActionsTriggerElement,
menuContainer: itemActionsMenuContainer,
},
}),
);

return domElement;
});
Expand Down Expand Up @@ -106,9 +144,16 @@ class IbexaEmbedContentInlineEditing extends Plugin {

findContent({ token, siteaccess, contentId }, (contents) => {
const contentName = contents[0].TranslatedName;
const locationId = contents[0].MainLocation._href.split('/').pop();
const languageCodes = contents[0].CurrentVersion.Version.VersionInfo.VersionTranslationInfo.Language.map(
(language) => language.languageCode,
);

this.editor.model.change((writer) => {
writer.setAttribute('contentName', contentName, modelElement);
writer.setAttribute('contentId', contentId, modelElement);
writer.setAttribute('locationId', locationId, modelElement);
writer.setAttribute('languageCodes', languageCodes, modelElement);
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,16 @@ class IbexaEmbedContentInlineUI extends IbexaEmbedBaseUI {
}

getCommandOptions(items) {
const location = items[0];
const content = location.ContentInfo.Content;

return {
contentId: items[0].ContentInfo.Content._id,
contentName: items[0].ContentInfo.Content.TranslatedName,
contentId: content._id,
contentName: content.TranslatedName,
locationId: location.id,
languageCodes: content.CurrentVersion.Version.VersionInfo.VersionTranslationInfo.Language.map(
(language) => language.languageCode,
),
};
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ class IbexaEmbedContentCommand extends Command {
});
}

createEmbed(writer, { contentId, contentName }) {
return writer.createElement('embed', { contentId, contentName });
createEmbed(writer, { contentId, contentName, locationId, languageCodes }) {
return writer.createElement('embed', { contentId, contentName, locationId, languageCodes });
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,27 @@ import IbexaEmbedContentCommand from './embed-command';

import { findContent } from '../../services/content-service';

const renderPreview = (title) => {
const renderPreview = (title, contentId) => {
return `<svg class="ibexa-icon ibexa-icon--medium ibexa-icon--secondary">
<use xlink:href="${window.ibexa.helpers.icon.getIconPath('embed')}"></use>
</svg>
<span class="ibexa-embed-content__title">${title}</span>`;
<use xlink:href="${window.ibexa.helpers.icon.getIconPath('embed')}"></use>
</svg>
<span
class="ibexa-embed-content__title"
data-ibexa-update-content-id="${contentId}"
data-ibexa-update-source-data-path="Content.Name"
>
${title}
</span>
<span>
<button
type="button"
class="btn ibexa-btn ibexa-btn--small ibexa-btn--ghost ibexa-btn--no-text ibexa-embedded-item-actions__menu-trigger-btn"
>
<svg class="ibexa-icon ibexa-icon--small ibexa-icon--secondary">
<use xlink:href="${window.ibexa.helpers.icon.getIconPath('options')}"></use>
</svg>
</button>
</span>`;
};

class IbexaEmbedContentEditing extends Plugin {
Expand All @@ -24,7 +40,7 @@ class IbexaEmbedContentEditing extends Plugin {
schema.register('embed', {
isObject: true,
allowWhere: '$block',
allowAttributes: ['contentId', 'contentName'],
allowAttributes: ['contentId', 'contentName', 'locationId', 'languageCodes'],
});
}

Expand All @@ -43,9 +59,11 @@ class IbexaEmbedContentEditing extends Plugin {
class: 'ibexa-embed',
});
const preview = downcastWriter.createUIElement('p', { class: 'ibexa-embed-content' }, function (domDocument) {
const contentId = modelElement.getAttribute('contentId');
const contentName = modelElement.getAttribute('contentName');
const domElement = this.toDomElement(domDocument);

domElement.innerHTML = renderPreview(modelElement.getAttribute('contentName'));
domElement.innerHTML = renderPreview(contentName, contentId);

return domElement;
});
Expand All @@ -57,13 +75,35 @@ class IbexaEmbedContentEditing extends Plugin {
})
.add((dispatcher) =>
dispatcher.on('attribute:contentName', (event, data, conversionApi) => {
const { editor } = this;
const downcastWriter = conversionApi.writer;
const modelElement = data.item;
const viewElement = conversionApi.mapper.toViewElement(modelElement);
const preview = downcastWriter.createUIElement('p', { class: 'ibexa-embed-content' }, function (domDocument) {
const contentId = modelElement.getAttribute('contentId');
const contentName = modelElement.getAttribute('contentName');
const locationId = modelElement.getAttribute('locationId');
const languageCodes = modelElement.getAttribute('languageCodes');
const domElement = this.toDomElement(domDocument);

domElement.innerHTML = renderPreview(modelElement.getAttribute('contentName'));
domElement.innerHTML = renderPreview(contentName);

const itemActionsTriggerElement = domElement.querySelector('.ibexa-embedded-item-actions__menu-trigger-btn');
const itemActionsMenuContainer = editor.sourceElement.parentNode.querySelector(
'.ibexa-embedded-item-actions .ibexa-multilevel-popup-menu',
);

domDocument.body.dispatchEvent(
new CustomEvent('ibexa-embedded-item:create-dynamic-menu', {
detail: {
contentId,
locationId,
languageCodes,
menuTriggerElement: itemActionsTriggerElement,
menuContainer: itemActionsMenuContainer,
},
}),
);

return domElement;
});
Expand Down Expand Up @@ -106,9 +146,16 @@ class IbexaEmbedContentEditing extends Plugin {

findContent({ token, siteaccess, contentId }, (contents) => {
const contentName = contents[0].TranslatedName;
const locationId = contents[0].MainLocation._href.split('/').pop();
const languageCodes = contents[0].CurrentVersion.Version.VersionInfo.VersionTranslationInfo.Language.map(
(language) => language.languageCode,
);

this.editor.model.change((writer) => {
writer.setAttribute('contentName', contentName, modelElement);
writer.setAttribute('contentId', contentId, modelElement);
writer.setAttribute('locationId', locationId, modelElement);
writer.setAttribute('languageCodes', languageCodes, modelElement);
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,16 @@ class IbexaEmbedContentUI extends IbexaEmbedBaseUI {
}

getCommandOptions(items) {
const location = items[0];
const content = location.ContentInfo.Content;

return {
contentId: items[0].ContentInfo.Content._id,
contentName: items[0].ContentInfo.Content.TranslatedName,
contentId: content._id,
contentName: content.TranslatedName,
locationId: location.id,
languageCodes: content.CurrentVersion.Version.VersionInfo.VersionTranslationInfo.Language.map(
(language) => language.languageCode,
),
};
}
}
Expand Down

0 comments on commit cbae649

Please sign in to comment.