Skip to content

Commit

Permalink
!!!TASK: Simplify legacy autoparagraph: false mode
Browse files Browse the repository at this point in the history
The outer tag is not removed anymore - instead youll get a span. You should switch to the new isInline mode
  • Loading branch information
mhsdesign committed Jun 29, 2023
1 parent 169c750 commit 429f2e4
Show file tree
Hide file tree
Showing 4 changed files with 2 additions and 71 deletions.
Expand Up @@ -5,24 +5,5 @@ export const cleanupContentBeforeCommit = content => {
if (content.match(/^<([a-z][a-z0-9]*)\b[^>]*>&nbsp;<\/\1>$/)) {
return '';
}

if (content.includes('<neos-inline-wrapper>')) {
let contentWithoutOuterInlineWrapper = content;

if (content.startsWith('<neos-inline-wrapper>') && content.endsWith('</neos-inline-wrapper>')) {
contentWithoutOuterInlineWrapper = content
.replace(/^<neos-inline-wrapper>/, '')
.replace(/<\/neos-inline-wrapper>$/, '');
}

if (contentWithoutOuterInlineWrapper.includes('<neos-inline-wrapper>')) {
// in the case, multiple root paragraph elements were inserted into the ckeditor (wich is currently not prevented if the html is modified from outside)
// we have multiple root elements of type <neos-inline-wrapper>. We will convert all of them into spans.
return content
.replace(/<neos-inline-wrapper>/g, '<span>')
.replace(/<\/neos-inline-wrapper>/g, '</span>');
}
return contentWithoutOuterInlineWrapper;
}
return content;
};
Expand Up @@ -8,29 +8,3 @@ test('remove empty nbsp', () => {
assertCleanedUpContent('<p>&nbsp;</p>', '');
assertCleanedUpContent('<span>&nbsp;</span>', '');
})

describe('ckeditor inline mode hack, cleanup <neos-inline-wrapper>', () => {
test('noop', () => {
assertCleanedUpContent('<p></p>', '<p></p>');

assertCleanedUpContent('', '');
})

test('cleanup single <neos-inline-wrapper>', () => {
assertCleanedUpContent('<neos-inline-wrapper></neos-inline-wrapper>', '');
assertCleanedUpContent('<neos-inline-wrapper>foo</neos-inline-wrapper>', 'foo');

assertCleanedUpContent('<neos-inline-wrapper><span>foo</span></neos-inline-wrapper>', '<span>foo</span>');
})

test('cleanup multiple <neos-inline-wrapper>', () => {
assertCleanedUpContent('<neos-inline-wrapper>foo</neos-inline-wrapper><neos-inline-wrapper>bar</neos-inline-wrapper>', '<span>foo</span><span>bar</span>');

assertCleanedUpContent('<neos-inline-wrapper>foo</neos-inline-wrapper><neos-inline-wrapper>bar</neos-inline-wrapper>', '<span>foo</span><span>bar</span>');
})

test('cleanup <neos-inline-wrapper> after other root', () => {
// in the case you had multiple paragraphs and a headline and switched to autoparagrahp: false
assertCleanedUpContent('<h1>foo</h1><neos-inline-wrapper>bar</neos-inline-wrapper>', '<h1>foo</h1><span>bar</span>');
})
})
14 changes: 1 addition & 13 deletions packages/neos-ui-ckeditor5-bindings/src/manifest.config.js
Expand Up @@ -33,18 +33,6 @@ const addPlugin = (Plugin, isEnabled) => (ckEditorConfiguration, options) => {
return ckEditorConfiguration;
};

// If the editable is a span or a heading, we automatically disable paragraphs and enable the soft break mode
// Also possible to force this behavior with `autoparagraph: false`
const disableAutoparagraph = (editorOptions, {propertyDomNode}) =>
$get('autoparagraph', editorOptions) === false ||
propertyDomNode.tagName === 'SPAN' ||
propertyDomNode.tagName === 'H1' ||
propertyDomNode.tagName === 'H2' ||
propertyDomNode.tagName === 'H3' ||
propertyDomNode.tagName === 'H4' ||
propertyDomNode.tagName === 'H5' ||
propertyDomNode.tagName === 'H6';

//
// Create richtext editing toolbar registry
//
Expand Down Expand Up @@ -98,7 +86,7 @@ export default ckEditorRegistry => {
config.set('essentials', addPlugin(Essentials));
config.set('paragraph', addPlugin(Paragraph));
// @deprecated
config.set('disabledAutoparagraphMode', addPlugin(DisabledAutoparagraphMode, disableAutoparagraph));
config.set('disabledAutoparagraphMode', addPlugin(DisabledAutoparagraphMode, (editorOptions) => editorOptions?.autoparagraph === false));
config.set('neosPlaceholder', addPlugin(NeosPlaceholder));
config.set('sub', addPlugin(Sub, $get('formatting.sub')));
config.set('sup', addPlugin(Sup, $get('formatting.sup')));
Expand Down
Expand Up @@ -13,19 +13,7 @@ export default class DisabledAutoparagraphMode extends Plugin {
const {editor} = this;

// we map paragraph model into plain <span> element in edit mode
editor.conversion.for('editingDowncast').elementToElement({model: 'paragraph', view: 'span', converterPriority: 'high'});

// to avoid having a wrapping "span" tag, we will convert the outmost 'paragraph' and strip the custom tag 'neos-inline-wrapper'
// in a hacky cleanup in cleanupContentBeforeCommit
// see https://neos-project.slack.com/archives/C07QEQ1U2/p1687952441254759 - i could find a better solution
editor.conversion.for('dataDowncast').elementToElement({model: 'paragraph', view: (modelElement, viewWriter) => {
const parentIsRoot = modelElement.parent.is('$root');
const hasAttributes = [...modelElement.getAttributes()].length !== 0;
if (!parentIsRoot || hasAttributes) {
return viewWriter.createContainerElement('span');
}
return viewWriter.createContainerElement('neos-inline-wrapper');
}, converterPriority: 'high'});
editor.conversion.for('downcast').elementToElement({model: 'paragraph', view: 'span', converterPriority: 'high'});

// we redefine enter key to create soft breaks (<br>) instead of new paragraphs
editor.editing.view.document.on('enter', (evt, data) => {
Expand Down

0 comments on commit 429f2e4

Please sign in to comment.