| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- /**
- * @license Copyright (c) 2003-2018, 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 Bold from '@ckeditor/ckeditor5-basic-styles/src/bold';
- import Italic from '@ckeditor/ckeditor5-basic-styles/src/italic';
- import List from '@ckeditor/ckeditor5-list/src/list';
- 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 schema = editor.model.schema;
- const data = editor.data;
- const editing = editor.editing;
- // Configure schema.
- schema.register( 'fancywidget', {
- isObject: true
- } );
- schema.extend( 'fancywidget', { allowIn: '$root' } );
- // Build converter from model to view for editing pipeline.
- buildModelConverter().for( editing.modelToView )
- .fromElement( 'fancywidget' )
- .toElement( () => {
- const widgetElement = new ViewContainerElement( 'figure', { class: 'fancy-widget' }, new ViewText( 'widget' ) );
- return toWidget( widgetElement );
- } );
- // 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, Bold, Italic, List, FancyWidget ],
- toolbar: [ 'headings', 'undo', 'redo', 'bold', 'italic', 'numberedList', 'bulletedList' ]
- } )
- .then( editor => {
- window.editor = editor;
- buildModelConverter()
- .for( editor.editing.modelToView )
- .fromMarker( 'marker' )
- .toHighlight( data => ( { class: 'highlight-' + data.markerName.split( ':' )[ 1 ] } ) );
- document.getElementById( 'add-marker-yellow' ).addEventListener( 'mousedown', evt => {
- addMarker( editor, 'yellow' );
- evt.preventDefault();
- } );
- document.getElementById( 'add-marker-blue' ).addEventListener( 'mousedown', evt => {
- addMarker( editor, 'blue' );
- evt.preventDefault();
- } );
- document.getElementById( 'add-marker-red' ).addEventListener( 'mousedown', evt => {
- addMarker( editor, 'red' );
- evt.preventDefault();
- } );
- document.getElementById( 'remove-marker-yellow' ).addEventListener( 'mousedown', evt => {
- removeMarker( editor, 'yellow' );
- evt.preventDefault();
- } );
- document.getElementById( 'remove-marker-blue' ).addEventListener( 'mousedown', evt => {
- removeMarker( editor, 'blue' );
- evt.preventDefault();
- } );
- document.getElementById( 'remove-marker-red' ).addEventListener( 'mousedown', evt => {
- removeMarker( editor, 'red' );
- evt.preventDefault();
- } );
- document.getElementById( 'remove-markers' ).addEventListener( 'mousedown', evt => {
- const markers = editor.model.markers;
- editor.model.change( () => {
- for ( const marker of markers ) {
- markers.remove( marker );
- }
- } );
- evt.preventDefault();
- } );
- } )
- .catch( err => {
- console.error( err.stack );
- } );
- function addMarker( editor, color ) {
- editor.model.change( () => {
- const range = ModelRange.createFromRange( editor.model.document.selection.getFirstRange() );
- editor.model.markers.set( 'marker:' + color, range );
- } );
- }
- function removeMarker( editor, color ) {
- editor.model.change( () => {
- editor.model.markers.remove( 'marker:' + color );
- } );
- }
|