|
@@ -12,16 +12,109 @@ import Mention from '../../src/mention';
|
|
|
import Underline from '@ckeditor/ckeditor5-basic-styles/src/underline';
|
|
import Underline from '@ckeditor/ckeditor5-basic-styles/src/underline';
|
|
|
import ArticlePluginSet from '@ckeditor/ckeditor5-core/tests/_utils/articlepluginset';
|
|
import ArticlePluginSet from '@ckeditor/ckeditor5-core/tests/_utils/articlepluginset';
|
|
|
import Font from '@ckeditor/ckeditor5-font/src/font';
|
|
import Font from '@ckeditor/ckeditor5-font/src/font';
|
|
|
|
|
+import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
|
|
|
|
|
+
|
|
|
|
|
+import { toWidget, viewToModelPositionOutsideModelElement } from '@ckeditor/ckeditor5-widget/src/utils';
|
|
|
|
|
+import ButtonView from '@ckeditor/ckeditor5-ui/src/button/buttonview';
|
|
|
|
|
+
|
|
|
|
|
+class InlineWidget extends Plugin {
|
|
|
|
|
+ constructor( editor ) {
|
|
|
|
|
+ super( editor );
|
|
|
|
|
+
|
|
|
|
|
+ editor.model.schema.register( 'placeholder', {
|
|
|
|
|
+ allowWhere: '$text',
|
|
|
|
|
+ isObject: true,
|
|
|
|
|
+ isInline: true,
|
|
|
|
|
+ allowAttributes: [ 'type' ]
|
|
|
|
|
+ } );
|
|
|
|
|
+
|
|
|
|
|
+ editor.conversion.for( 'editingDowncast' ).elementToElement( {
|
|
|
|
|
+ model: 'placeholder',
|
|
|
|
|
+ view: ( modelItem, viewWriter ) => {
|
|
|
|
|
+ const widgetElement = createPlaceholderView( modelItem, viewWriter );
|
|
|
|
|
+
|
|
|
|
|
+ return toWidget( widgetElement, viewWriter );
|
|
|
|
|
+ }
|
|
|
|
|
+ } );
|
|
|
|
|
+
|
|
|
|
|
+ editor.conversion.for( 'dataDowncast' ).elementToElement( {
|
|
|
|
|
+ model: 'placeholder',
|
|
|
|
|
+ view: createPlaceholderView
|
|
|
|
|
+ } );
|
|
|
|
|
+
|
|
|
|
|
+ editor.conversion.for( 'upcast' ).elementToElement( {
|
|
|
|
|
+ view: 'placeholder',
|
|
|
|
|
+ model: ( viewElement, modelWriter ) => {
|
|
|
|
|
+ let type = 'general';
|
|
|
|
|
+
|
|
|
|
|
+ if ( viewElement.childCount ) {
|
|
|
|
|
+ const text = viewElement.getChild( 0 );
|
|
|
|
|
+
|
|
|
|
|
+ if ( text.is( 'text' ) ) {
|
|
|
|
|
+ type = text.data.slice( 1, -1 );
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return modelWriter.createElement( 'placeholder', { type } );
|
|
|
|
|
+ }
|
|
|
|
|
+ } );
|
|
|
|
|
+
|
|
|
|
|
+ editor.editing.mapper.on(
|
|
|
|
|
+ 'viewToModelPosition',
|
|
|
|
|
+ viewToModelPositionOutsideModelElement( editor.model, viewElement => viewElement.name == 'placeholder' )
|
|
|
|
|
+ );
|
|
|
|
|
+
|
|
|
|
|
+ this._createToolbarButton();
|
|
|
|
|
+
|
|
|
|
|
+ function createPlaceholderView( modelItem, viewWriter ) {
|
|
|
|
|
+ const widgetElement = viewWriter.createContainerElement( 'placeholder' );
|
|
|
|
|
+ const viewText = viewWriter.createText( '{' + modelItem.getAttribute( 'type' ) + '}' );
|
|
|
|
|
+
|
|
|
|
|
+ viewWriter.insert( viewWriter.createPositionAt( widgetElement, 0 ), viewText );
|
|
|
|
|
+
|
|
|
|
|
+ return widgetElement;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ _createToolbarButton() {
|
|
|
|
|
+ const editor = this.editor;
|
|
|
|
|
+ const t = editor.t;
|
|
|
|
|
+
|
|
|
|
|
+ editor.ui.componentFactory.add( 'placeholder', locale => {
|
|
|
|
|
+ const buttonView = new ButtonView( locale );
|
|
|
|
|
+
|
|
|
|
|
+ buttonView.set( {
|
|
|
|
|
+ label: t( 'Insert placeholder' ),
|
|
|
|
|
+ tooltip: true,
|
|
|
|
|
+ withText: true
|
|
|
|
|
+ } );
|
|
|
|
|
+
|
|
|
|
|
+ this.listenTo( buttonView, 'execute', () => {
|
|
|
|
|
+ const model = editor.model;
|
|
|
|
|
+
|
|
|
|
|
+ model.change( writer => {
|
|
|
|
|
+ const placeholder = writer.createElement( 'placeholder', { type: 'placeholder' } );
|
|
|
|
|
+
|
|
|
|
|
+ model.insertContent( placeholder );
|
|
|
|
|
+
|
|
|
|
|
+ writer.setSelection( placeholder, 'on' );
|
|
|
|
|
+ } );
|
|
|
|
|
+ } );
|
|
|
|
|
+
|
|
|
|
|
+ return buttonView;
|
|
|
|
|
+ } );
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
ClassicEditor
|
|
ClassicEditor
|
|
|
.create( global.document.querySelector( '#editor' ), {
|
|
.create( global.document.querySelector( '#editor' ), {
|
|
|
- plugins: [ ArticlePluginSet, Underline, Font, Mention ],
|
|
|
|
|
|
|
+ plugins: [ ArticlePluginSet, Underline, Font, Mention, InlineWidget ],
|
|
|
toolbar: [
|
|
toolbar: [
|
|
|
'heading',
|
|
'heading',
|
|
|
'|', 'bulletedList', 'numberedList', 'blockQuote',
|
|
'|', 'bulletedList', 'numberedList', 'blockQuote',
|
|
|
'|', 'bold', 'italic', 'underline', 'link',
|
|
'|', 'bold', 'italic', 'underline', 'link',
|
|
|
'|', 'fontFamily', 'fontSize', 'fontColor', 'fontBackgroundColor',
|
|
'|', 'fontFamily', 'fontSize', 'fontColor', 'fontBackgroundColor',
|
|
|
- '|', 'insertTable',
|
|
|
|
|
|
|
+ '|', 'insertTable', 'placeholder',
|
|
|
'|', 'undo', 'redo'
|
|
'|', 'undo', 'redo'
|
|
|
],
|
|
],
|
|
|
image: {
|
|
image: {
|