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

IBX-7762: Fixed anchor in list #150

Merged
merged 5 commits into from
Mar 14, 2024
Merged
Changes from 1 commit
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
51 changes: 51 additions & 0 deletions src/bundle/Resources/public/js/CKEditor/anchor/anchor-editing.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,57 @@ class IbexaAnchorEditing extends Plugin {
key: 'id',
},
});

this.editor.conversion.for('dataDowncast').add((dispatcher) => {
dispatcher.on('attribute:anchor:listItem', (event, data, conversionApi) => {
if (data.attributeKey !== 'anchor' || data.attributeNewValue === '') {
dew326 marked this conversation as resolved.
Show resolved Hide resolved
return;
}

const viewItem = conversionApi.mapper.toViewElement(data.item);
const previousElement = viewItem.parent.previousSibling;

conversionApi.writer.setAttribute('id', data.attributeNewValue, viewItem.parent);
conversionApi.writer.removeAttribute('id', viewItem);

if (previousElement?.name === viewItem.parent.name) {
dew326 marked this conversation as resolved.
Show resolved Hide resolved
conversionApi.writer.mergeContainers(conversionApi.writer.createPositionAfter(previousElement));
}
});
});

this.editor.conversion.for('editingDowncast').add((dispatcher) => {
dispatcher.on('attribute:anchor:listItem', (event, data, conversionApi) => {
if (data.attributeKey !== 'anchor' || data.attributeNewValue === '') {
return;
}

const viewItem = conversionApi.mapper.toViewElement(data.item);
const previousElement = viewItem.parent.previousSibling;
const nextElement = viewItem.parent.nextSibling;

conversionApi.writer.setAttribute('id', data.attributeNewValue, viewItem.parent);
conversionApi.writer.removeAttribute('id', viewItem);

if (previousElement?.name === viewItem.parent.name) {
conversionApi.writer.mergeContainers(conversionApi.writer.createPositionAfter(previousElement));
}

if (nextElement?.name === viewItem.parent.name) {
conversionApi.writer.mergeContainers(conversionApi.writer.createPositionBefore(nextElement));
}
});
});

this.editor.conversion.for('upcast').add((dispatcher) => {
dispatcher.on('element:li', (event, data, conversionApi) => {
const listParent = data.viewItem.parent;
const listItem = data.modelRange.start.nodeAfter || data.modelRange.end.nodeBefore;
dew326 marked this conversation as resolved.
Show resolved Hide resolved
const classes = listParent.getAttribute('id');
dew326 marked this conversation as resolved.
Show resolved Hide resolved

conversionApi.writer.setAttribute('anchor', classes, listItem);
});
});
}

init() {
Expand Down
Loading