|
|
@@ -0,0 +1,93 @@
|
|
|
+/**
|
|
|
+ * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
|
|
|
+ * For licensing, see LICENSE.md.
|
|
|
+ */
|
|
|
+
|
|
|
+/* global console, window, document */
|
|
|
+
|
|
|
+import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classiceditor';
|
|
|
+import Enter from '@ckeditor/ckeditor5-enter/src/enter';
|
|
|
+import Typing from '@ckeditor/ckeditor5-typing/src/typing';
|
|
|
+import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
|
|
|
+import Undo from '@ckeditor/ckeditor5-undo/src/undo';
|
|
|
+import Heading from '@ckeditor/ckeditor5-heading/src/heading';
|
|
|
+import global from '@ckeditor/ckeditor5-utils/src/dom/global';
|
|
|
+import buildModelConverter from '../../src/conversion/buildmodelconverter';
|
|
|
+import buildViewConverter from '../../src/conversion/buildviewconverter';
|
|
|
+import ModelRange from '../../src/model/range';
|
|
|
+import ModelElement from '../../src/model/element';
|
|
|
+import ViewContainerElement from '../../src/view/containerelement';
|
|
|
+import ViewText from '../../src/view/text';
|
|
|
+import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
|
|
|
+import Widget from '@ckeditor/ckeditor5-widget/src/widget';
|
|
|
+import { toWidget } from '@ckeditor/ckeditor5-widget/src/utils';
|
|
|
+
|
|
|
+class FancyWidget extends Plugin {
|
|
|
+ static get requires() {
|
|
|
+ return [ Widget ];
|
|
|
+ }
|
|
|
+
|
|
|
+ init() {
|
|
|
+ const editor = this.editor;
|
|
|
+ const doc = editor.document;
|
|
|
+ const schema = doc.schema;
|
|
|
+ const data = editor.data;
|
|
|
+ const editing = editor.editing;
|
|
|
+
|
|
|
+ // Configure schema.
|
|
|
+ schema.registerItem( 'fancywidget' );
|
|
|
+ schema.allow( { name: 'fancywidget', inside: '$root' } );
|
|
|
+ schema.objects.add( 'fancywidget' );
|
|
|
+
|
|
|
+ // Build converter from model to view for editing pipeline.
|
|
|
+ buildModelConverter().for( editing.modelToView )
|
|
|
+ .fromElement( 'fancywidget' )
|
|
|
+ .toElement( () => {
|
|
|
+ const text = new ViewText( 'widget' );
|
|
|
+ const fancyView = new ViewContainerElement( 'figure', { class: 'fancy-widget' }, text );
|
|
|
+
|
|
|
+ fancyView.setVirtualSelection = data => {
|
|
|
+ text.data = 'widget - with virtual selection';
|
|
|
+ fancyView.addClass( data.class );
|
|
|
+ };
|
|
|
+
|
|
|
+ fancyView.removeVirtualSelection = data => {
|
|
|
+ text.data = 'widget';
|
|
|
+ fancyView.removeClass( data.class );
|
|
|
+ };
|
|
|
+
|
|
|
+ return toWidget( fancyView );
|
|
|
+ } );
|
|
|
+
|
|
|
+ // Build converter from view element to model element for data pipeline.
|
|
|
+ buildViewConverter().for( data.viewToModel )
|
|
|
+ .fromElement( 'figure' )
|
|
|
+ .toElement( () => new ModelElement( 'fancywidget' ) );
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+ClassicEditor.create( global.document.querySelector( '#editor' ), {
|
|
|
+ plugins: [ Enter, Typing, Paragraph, Undo, Heading, FancyWidget ],
|
|
|
+ toolbar: [ 'headings', 'undo', 'redo' ]
|
|
|
+} )
|
|
|
+.then( editor => {
|
|
|
+ window.editor = editor;
|
|
|
+ const model = editor.document;
|
|
|
+
|
|
|
+ buildModelConverter()
|
|
|
+ .for( editor.editing.modelToView )
|
|
|
+ .fromMarker( 'marker' )
|
|
|
+ .toVirtualSelection( { class: 'virtual-selection' } );
|
|
|
+
|
|
|
+ document.getElementById( 'add-marker' ).addEventListener( 'mousedown', evt => {
|
|
|
+ editor.document.enqueueChanges( () => {
|
|
|
+ const range = ModelRange.createFromRange( model.selection.getFirstRange() );
|
|
|
+ model.markers.set( 'marker', range );
|
|
|
+ } );
|
|
|
+
|
|
|
+ evt.preventDefault();
|
|
|
+ } );
|
|
|
+} )
|
|
|
+.catch( err => {
|
|
|
+ console.error( err.stack );
|
|
|
+} );
|