import Plugin from '@ckeditor/ckeditor5-core/src/plugin'; import ButtonView from '@ckeditor/ckeditor5-ui/src/button/buttonview'; import { $gettext } from '../../lib/gettext'; import { Command, icons } from '@ckeditor/ckeditor5-core'; const divideIcon = ''; const removeIcon = ''; export default class StudipBlockQuote extends Plugin { init() { const editor = this.editor; editor.commands.add('insertStudipQuote', new InsertStudipQuoteCommand(editor)); editor.commands.add('splitStudipQuote', new SplitStudipQuoteCommand(editor)); editor.commands.add('removeStudipQuote', new RemoveStudipQuoteCommand(editor)); editor.ui.componentFactory.add('insertBlockQuote', (locale) => { const view = new ButtonView(locale); view.set({ label: $gettext('Zitat einfügen'), icon: icons.quote, tooltip: true, }); const command = editor.commands.get('removeStudipQuote'); view.bind('isEnabled').to(command, 'isEnabled'); // Callback executed once the image is clicked. view.on('execute', () => { editor.execute('insertStudipQuote'); }); return view; }); editor.ui.componentFactory.add('splitBlockQuote', (locale) => { const view = new ButtonView(locale); view.set({ label: $gettext('Zitat teilen'), icon: divideIcon, keystroke: 'Ctrl+Shift+Enter', tooltip: true, withText: false, }); const command = editor.commands.get('removeStudipQuote'); view.bind('isEnabled').to(command, 'isEnabled'); // Callback executed once the image is clicked. view.on('execute', () => { editor.execute('splitStudipQuote'); }); return view; }); editor.ui.componentFactory.add('removeBlockQuote', (locale) => { const view = new ButtonView(locale); view.set({ label: $gettext('Zitat löschen'), icon: removeIcon, tooltip: true, withText: false, }); const command = editor.commands.get('removeStudipQuote'); view.bind('isEnabled').to(command, 'isEnabled'); // Callback executed once the image is clicked. view.on('execute', () => { editor.execute('removeStudipQuote'); }); return view; }); } } class InsertStudipQuoteCommand extends Command { execute() { var writtenBy = $gettext('%s hat geschrieben:'); const content = '
' + writtenBy.replace('%s', $gettext('"Name"')) + '

 

 

'; const viewFragment = this.editor.data.processor.toView(content); const modelFragment = this.editor.data.toModel(viewFragment); this.editor.model.insertContent(modelFragment); } } class SplitStudipQuoteCommand extends Command { execute() { const position = this.editor.model.document.selection.getFirstPosition(); const quote = position.findAncestor('blockQuote'); if (quote !== null) { this.editor.model.change((writer) => { const limitElement = quote.parent; const split = writer.split(position, limitElement); writer.insertElement('paragraph', split.position); }); } } } class RemoveStudipQuoteCommand extends Command { execute() { const position = this.editor.model.document.selection.getFirstPosition(); const quote = position.findAncestor('blockQuote'); if (quote !== null) { this.editor.model.change((writer) => { // Remove the top "written by" bar for (var child of quote.getChildren()) { if ( child.is('element', 'htmlDivParagraph') && child.getAttribute('htmlAttributes').classes.includes('author') ) { writer.remove(child); } } // Only remove the current quote - save all children const range = writer.createRangeIn(quote); writer.move(range, quote, 'after'); writer.remove(quote); }); } } static get pluginName() { return 'StudipBlockQuote'; } }