Skip to content

Commit

Permalink
IBX-7762: Fixed anchor in list (#150)
Browse files Browse the repository at this point in the history
  • Loading branch information
dew326 authored Mar 14, 2024
1 parent 994b125 commit bcca58d
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 1 deletion.
61 changes: 61 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,67 @@ 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') {
return;
}

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

if (data.attributeNewValue) {
conversionApi.writer.setAttribute('id', data.attributeNewValue, viewItem.parent);
} else {
conversionApi.writer.removeAttribute('id', 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') {
return;
}

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

if (data.attributeNewValue) {
conversionApi.writer.setAttribute('id', data.attributeNewValue, viewItem.parent);
} else {
conversionApi.writer.removeAttribute('id', 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 id = listParent.getAttribute('id');

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

init() {
Expand Down
21 changes: 21 additions & 0 deletions src/bundle/Resources/public/js/CKEditor/anchor/anchor-ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,22 @@ class IbexaAnchorUI extends Plugin {
return this.editor.model.document.selection.getSelectedElement() || this.editor.model.document.selection.anchor.parent;
}

removeAnchorFromSiblings(modelElement, writer) {
let { previousSibling, nextSibling } = modelElement;

while (previousSibling?.name === 'listItem') {
writer.removeAttribute('anchor', previousSibling);

previousSibling = previousSibling.previousSibling;

Check failure on line 30 in src/bundle/Resources/public/js/CKEditor/anchor/anchor-ui.js

View workflow job for this annotation

GitHub Actions / Frontend build test

Use object destructuring
}

while (nextSibling?.name === 'listItem') {
writer.removeAttribute('anchor', nextSibling);

nextSibling = nextSibling.nextSibling;

Check failure on line 36 in src/bundle/Resources/public/js/CKEditor/anchor/anchor-ui.js

View workflow job for this annotation

GitHub Actions / Frontend build test

Use object destructuring
}
}

createFormView() {
const formView = new IbexaAnchorFormView({ locale: this.editor.locale });

Expand Down Expand Up @@ -56,9 +72,14 @@ class IbexaAnchorUI extends Plugin {

this.listenTo(formView, 'remove-anchor', () => {
const modelElement = this.getModelElement();
const isListItem = modelElement.name === 'listItem';

this.editor.model.change((writer) => {
writer.removeAttribute('anchor', modelElement);

if (isListItem) {
this.removeAnchorFromSiblings(modelElement, writer);
}
});

this.hideForm();
Expand Down
2 changes: 1 addition & 1 deletion src/bundle/Resources/public/scss/_general.scss
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
border: calculateRem(1px) solid $ibexa-color-dark-200;
border-radius: $ibexa-border-radius;

.ck-content {
.ck.ck-content {
min-height: calculateRem(100px);
padding: 0 calculateRem(24px);
}
Expand Down

0 comments on commit bcca58d

Please sign in to comment.