Skip to content

Commit

Permalink
IBX-7762: Fixed anchor in list
Browse files Browse the repository at this point in the history
  • Loading branch information
dew326 committed Mar 5, 2024
1 parent fa7f5f0 commit 272500a
Showing 1 changed file with 51 additions and 0 deletions.
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 === '') {
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) {
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;
const classes = listParent.getAttribute('id');

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

init() {
Expand Down

0 comments on commit 272500a

Please sign in to comment.