ckeditor5 icon indicating copy to clipboard operation
ckeditor5 copied to clipboard

Support for details and summary elements

Open castroCrea opened this issue 5 years ago • 10 comments

📝 Provide a description of the new feature

What is the expected behavior of the proposed feature?

Someone ever try to do a details/summary element

image

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/details

I tried with no success

import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
import Command from '@ckeditor/ckeditor5-core/src/command';

export default class DetailSummary extends Plugin {
	static get requires() {
		return [DetailSummaryEditing];
	}
}

class DetailSummaryEditing extends Plugin {
	init() {
		this._defineSchema();
		this._defineConverters();
		this.editor.commands.add(
			'insertDetailSummary',
			new InsertDetailSummaryCommand(this.editor)
		);
	}

	_defineSchema() {
		// ADDED
		const schema = this.editor.model.schema;
		schema.register('detailSummary', {
			// Behaves like a self-contained object (e.g. an image).
			isObject: true,

			// Allow in places where other blocks are allowed (e.g. directly in the root).
			allowWhere: '$block',
			allowAttributes: ['data-uuid']
		});

		schema.register('summary', {
			// Cannot be split or left by the caret.
			isLimit: true,

			allowIn: 'detailSummary',

			// Allow content which is allowed in blocks (i.e. text with attributes).
			allowContentOf: '$block'
		});

		schema.register('detailContent', {
			// Cannot be split or left by the caret.
			isLimit: true,

			allowIn: 'detailSummary',

			// Allow content which is allowed in the root (e.g. paragraphs).
			allowContentOf: '$root'
		});



		schema.addChildCheck((context, childDefinition) => {
			if (
				context.endsWith('detailSummary') &&
				childDefinition.name == 'detailSummary'
			) {
				return false;
			}
		});
	}

	_defineConverters() {
		const conversion = this.editor.conversion;
		conversion.for('upcast').elementToElement({
			model: "detailSummary",
			view: {
				name: 'details',
				classes: 'detail_summary'
			},
		});

		conversion.for('downcast').elementToElement({
			model: 'detailSummary',
			view: {
				name: 'details',
				classes: 'detail_summary'
			}
		});

		// <summary> converters
		conversion.for('upcast').elementToElement({
			model: 'summary',
			view: {
				name: 'summary',
				classes: 'summary'
			}
		});
		conversion.for('downcast').elementToElement({
			model: 'summary',
			view: {
				name: 'h1',
				classes: 'summary'
			}
		});

		// <div> converters detailContent
		conversion.for('upcast').elementToElement({
			model: 'detailContent',
			view: {
				name: 'div',
				classes: 'detail_content'
			}
		});
		conversion.for('downcast').elementToElement({
			model: 'detailContent',
			view: {
				name: 'div',
				classes: 'detail_content'
			}
		});

	}
}

class InsertDetailSummaryCommand extends Command {
	execute(options = {}) {
		this.editor.model.change(writer => {
			this.editor.model.insertContent(createDetailSummary(writer));
		});
	}

	refresh() {
		const model = this.editor.model;
		const selection = model.document.selection;
		const allowedIn = model.schema.findAllowedParent(
			selection.getFirstPosition(),
			'detailSummary'
		);

		this.isEnabled = allowedIn !== null;
	}
}

function createDetailSummary(writer) {
	const detailSummary = writer.createElement('detailSummary');
	const summary = writer.createElement('summary');
	const detailContent = writer.createElement('detailContent');


	writer.append(summary, detailSummary);
	writer.append(detailContent, detailSummary);

	writer.appendElement('paragraph', detailContent);

	return detailSummary;
}

If you'd like to see this feature implemented, add a 👍 reaction to this post.

castroCrea avatar Nov 13 '20 16:11 castroCrea

Hi, thanks for the report. I confirm it as a valid feature request.

Mgsy avatar Nov 23 '20 08:11 Mgsy

any news on this ?

castroCrea avatar Mar 27 '21 10:03 castroCrea

Hello, please implement this feature, especially for bullet list...

finzzz avatar Jun 04 '22 03:06 finzzz

There has been no activity on this issue for the past year. We've marked it as stale and will close it in 30 days. We understand it may still be relevant, so if you're interested in the solution, leave a comment or reaction under this issue.

CKEditorBot avatar Oct 12 '23 05:10 CKEditorBot

Need this in SharePoint Online, where CKEditor is used to power modern text web parts.

heinrich-ulbricht avatar Oct 12 '23 06:10 heinrich-ulbricht

Hi! I would love to see this feature implemented! Thx!

bonnepioche avatar Jan 13 '24 14:01 bonnepioche

+23 :+1: from #11068

Witoso avatar Jan 15 '24 07:01 Witoso

Hi @Witoso , CKEditor 5 team, I see that details tag is working in CKEditor 5 now. But 'summary' tag still doesn't work and CKEditor 5 strips off (deletes) the summary tag within the details tag. Any idea when this will be fixed ?

Details: Input HTML:

<details class="list">
    <summary class="list">
        <h2>Click here</h2>
    </summary>
<div>
    THIS IS DIV CONTENT
</div>
</details>

gets modified by CKEditor5 to

<details class="list">
    <h2>
        Click here
    </h2>
    <div>
        THIS IS DIV CONTENT
    </div>
</details> 

Note that this is a valid HTML as reported in MDN Documentation as seen here.

sudheerb avatar Jun 06 '24 06:06 sudheerb

What is the current status here?

mtness avatar Oct 22 '25 15:10 mtness

What is the current status here?

+1

urmyfaith avatar Nov 22 '25 03:11 urmyfaith