| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- /**
- * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
- */
- /* global console, window */
- import global from '@ckeditor/ckeditor5-utils/src/dom/global';
- import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classiceditor';
- import Mention from '../../src/mention';
- import Underline from '@ckeditor/ckeditor5-basic-styles/src/underline';
- import ArticlePluginSet from '@ckeditor/ckeditor5-core/tests/_utils/articlepluginset';
- 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, conversionApi ) => {
- const widgetElement = createPlaceholderView( modelItem, conversionApi );
- return toWidget( widgetElement, conversionApi.writer );
- }
- } );
- editor.conversion.for( 'dataDowncast' ).elementToElement( {
- model: 'placeholder',
- view: createPlaceholderView
- } );
- editor.conversion.for( 'upcast' ).elementToElement( {
- view: 'placeholder',
- model: ( viewElement, { writer } ) => {
- let type = 'general';
- if ( viewElement.childCount ) {
- const text = viewElement.getChild( 0 );
- if ( text.is( '$text' ) ) {
- type = text.data.slice( 1, -1 );
- }
- }
- return writer.createElement( 'placeholder', { type } );
- }
- } );
- editor.editing.mapper.on(
- 'viewToModelPosition',
- viewToModelPositionOutsideModelElement( editor.model, viewElement => viewElement.name == 'placeholder' )
- );
- this._createToolbarButton();
- function createPlaceholderView( modelItem, { writer } ) {
- const widgetElement = writer.createContainerElement( 'placeholder' );
- const viewText = writer.createText( '{' + modelItem.getAttribute( 'type' ) + '}' );
- writer.insert( writer.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;
- } );
- }
- }
- class MentionCommandSwitcher extends Plugin {
- init() {
- const editor = this.editor;
- const mentionCommand = editor.commands.get( 'mention' );
- editor.ui.componentFactory.add( 'toggleMentionCommand', locale => {
- const view = new ButtonView( locale );
- view.set( {
- label: 'Mentions',
- withText: true,
- isToggleable: true
- } );
- view.bind( 'isOn' ).to( mentionCommand, 'isEnabled' );
- this.listenTo( view, 'execute', () => {
- if ( mentionCommand.isEnabled ) {
- mentionCommand.forceDisabled( 'mentionCommandSwitcher' );
- } else {
- mentionCommand.clearForceDisabled( 'mentionCommandSwitcher' );
- }
- } );
- return view;
- } );
- }
- }
- ClassicEditor
- .create( global.document.querySelector( '#editor' ), {
- plugins: [ ArticlePluginSet, Underline, Font, Mention, InlineWidget, MentionCommandSwitcher ],
- toolbar: [
- 'heading',
- '|', 'bulletedList', 'numberedList', 'blockQuote',
- '|', 'bold', 'italic', 'underline', 'link',
- '|', 'fontFamily', 'fontSize', 'fontColor', 'fontBackgroundColor',
- '|', 'insertTable', 'placeholder', 'toggleMentionCommand',
- '|', 'undo', 'redo'
- ],
- image: {
- toolbar: [ 'imageStyle:full', 'imageStyle:side', '|', 'imageTextAlternative' ]
- },
- table: {
- contentToolbar: [ 'tableColumn', 'tableRow', 'mergeTableCells' ],
- tableToolbar: [ 'bold', 'italic' ]
- },
- mention: {
- feeds: [
- {
- marker: '@',
- feed: [ '@Barney', '@Lily', '@Marshall', '@Robin', '@Ted' ]
- },
- {
- marker: '#',
- feed: [
- '#a01', '#a02', '#a03', '#a04', '#a05', '#a06', '#a07', '#a08', '#a09', '#a10',
- '#a11', '#a12', '#a13', '#a14', '#a15', '#a16', '#a17', '#a18', '#a19', '#a20'
- ]
- },
- {
- marker: ':',
- feed: [
- ':+1:', ':-1:', ':@(at-sign):', ':$(dollar-sign):', ':#(hash-sign):'
- ]
- }
- ]
- }
- } )
- .then( editor => {
- window.editor = editor;
- } )
- .catch( err => {
- console.error( err.stack );
- } );
|