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
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
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) {
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') {
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 @@
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 @@

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
Loading