8
0
Просмотр исходного кода

Merge pull request #2 from ckeditor/i/5755

Feature: Implemented the `RestrictedEditing` plugin that brings the "restricted" mode. Closes ckeditor/ckeditor5#5755.
Aleksander Nowodzinski 6 лет назад
Родитель
Сommit
21790e1be1

+ 1 - 0
packages/ckeditor5-restricted-editing/package.json

@@ -17,6 +17,7 @@
     "@ckeditor/ckeditor5-engine": "^15.0.0",
     "@ckeditor/ckeditor5-paragraph": "^15.0.0",
     "@ckeditor/ckeditor5-table": "^15.0.0",
+    "@ckeditor/ckeditor5-utils": "^15.0.0",
     "eslint": "^5.5.0",
     "eslint-config-ckeditor5": "^2.0.0",
     "husky": "^2.4.1",

+ 90 - 0
packages/ckeditor5-restricted-editing/src/restrictedediting.js

@@ -0,0 +1,90 @@
+/**
+ * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
+ */
+
+/**
+ * @module restricted-editing/restrictedediting
+ */
+
+import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
+import Matcher from '@ckeditor/ckeditor5-engine/src/view/matcher';
+
+/**
+ * @extends module:core/plugin~Plugin
+ */
+export default class RestrictedEditing extends Plugin {
+	/**
+	 * @inheritDoc
+	 */
+	static get pluginName() {
+		return 'RestrictedEditing';
+	}
+
+	/**
+	 * @inheritDoc
+	 */
+	init() {
+		const editor = this.editor;
+
+		let createdMarkers = 0;
+
+		editor.conversion.for( 'upcast' ).add( upcastHighlightToMarker( {
+			view: {
+				name: 'span',
+				classes: 'ck-restricted-editing-exception'
+			},
+			model: () => {
+				createdMarkers++;
+
+				return `restricted-editing-exception:${ createdMarkers }`;
+			}
+		} ) );
+
+		editor.conversion.for( 'downcast' ).markerToHighlight( {
+			model: 'restricted-editing-exception',
+			// Use callback to return new object every time new marker instance is created - otherwise it will be seen as the same marker.
+			view: () => ( {
+				name: 'span',
+				classes: 'ck-restricted-editing-exception',
+				priority: -10
+			} )
+		} );
+	}
+}
+
+function upcastHighlightToMarker( config ) {
+	return dispatcher => dispatcher.on( 'element:span', ( evt, data, conversionApi ) => {
+		const { writer } = conversionApi;
+
+		const matcher = new Matcher( config.view );
+		const matcherResult = matcher.match( data.viewItem );
+
+		// If there is no match, this callback should not do anything.
+		if ( !matcherResult ) {
+			return;
+		}
+
+		const match = matcherResult.match;
+
+		// Force consuming element's name (taken from upcast helpers elementToElement converter).
+		match.name = true;
+
+		const { modelRange: convertedChildrenRange } = conversionApi.convertChildren( data.viewItem, data.modelCursor );
+		conversionApi.consumable.consume( data.viewItem, match );
+
+		const markerName = config.model( data.viewItem );
+		const fakeMarkerStart = writer.createElement( '$marker', { 'data-name': markerName } );
+		const fakeMarkerEnd = writer.createElement( '$marker', { 'data-name': markerName } );
+
+		// Insert in reverse order to use converter content positions directly (without recalculating).
+		writer.insert( fakeMarkerEnd, convertedChildrenRange.end );
+		writer.insert( fakeMarkerStart, convertedChildrenRange.start );
+
+		data.modelRange = writer.createRange(
+			writer.createPositionBefore( fakeMarkerStart ),
+			writer.createPositionAfter( fakeMarkerEnd )
+		);
+		data.modelCursor = data.modelRange.end;
+	} );
+}

+ 11 - 1
packages/ckeditor5-restricted-editing/src/restrictededitingexceptionediting.js

@@ -29,7 +29,7 @@ export default class RestrictedEditingExceptionEditing extends Plugin {
 
 		editor.model.schema.extend( '$text', { allowAttributes: [ 'restrictedEditingException' ] } );
 
-		editor.conversion.attributeToElement( {
+		editor.conversion.for( 'upcast' ).elementToAttribute( {
 			model: 'restrictedEditingException',
 			view: {
 				name: 'span',
@@ -37,6 +37,16 @@ export default class RestrictedEditingExceptionEditing extends Plugin {
 			}
 		} );
 
+		editor.conversion.for( 'downcast' ).attributeToElement( {
+			model: 'restrictedEditingException',
+			view: ( modelAttributeValue, viewWriter ) => {
+				if ( modelAttributeValue ) {
+					// Make the restricted editing <span> outer-most in the view.
+					return viewWriter.createAttributeElement( 'span', { class: 'ck-restricted-editing-exception' }, { priority: -10 } );
+				}
+			}
+		} );
+
 		editor.commands.add( 'restrictedEditingException', new RestrictedEditingExceptionCommand( editor ) );
 	}
 }

+ 25 - 0
packages/ckeditor5-restricted-editing/tests/manual/restrictedediting.html

@@ -1,3 +1,10 @@
+<p>
+	<button id="mode-standard">Switch to standard mode</button>
+	<button id="mode-restricted">Switch to restricted mode</button>
+</p>
+
+<p id="current-mode"></p>
+
 <div id="editor">
 	<h2>Heading 1</h2>
 	<p>Paragraph <span class="ck-restricted-editing-exception">it is editable</span></p>
@@ -28,4 +35,22 @@
 	.ck-restricted-editing-exception {
 		background-color: #ffcd96;
 	}
+
+	#current-mode {
+		/*text-align: center;*/
+		padding: 1em 0;
+	}
+
+	.mode {
+		color: #fff;
+		padding: 0.5em;
+	}
+
+	.mode-standard {
+		background: #4f4fff;
+	}
+
+	.mode-restricted {
+		background: #a72727;
+	}
 </style>

+ 51 - 11
packages/ckeditor5-restricted-editing/tests/manual/restrictedediting.js

@@ -3,19 +3,40 @@
  * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  */
 
-/* globals console, window, document */
+/* globals window, document */
 
 import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classiceditor';
 import ArticlePluginSet from '@ckeditor/ckeditor5-core/tests/_utils/articlepluginset';
 import Table from '@ckeditor/ckeditor5-table/src/table';
 
 import RestrictedEditingException from '../../src/restrictededitingexception';
+import RestrictedEditing from '../../src/restrictedediting';
 
-ClassicEditor
-	.create( document.querySelector( '#editor' ), {
+const restrictedModeButton = document.getElementById( 'mode-restricted' );
+const standardModeButton = document.getElementById( 'mode-standard' );
+const currentModeDisplay = document.getElementById( 'current-mode' );
+
+enableSwitchToStandardMode();
+enableSwitchToRestrictedMode();
+
+function enableSwitchToRestrictedMode() {
+	restrictedModeButton.removeAttribute( 'disabled' );
+	restrictedModeButton.addEventListener( 'click', startRestrictedMode );
+}
+
+function enableSwitchToStandardMode() {
+	standardModeButton.removeAttribute( 'disabled' );
+	standardModeButton.addEventListener( 'click', startStandardMode );
+}
+
+async function startStandardMode() {
+	standardModeButton.removeEventListener( 'click', startStandardMode );
+	standardModeButton.setAttribute( 'disabled', 'disabled' );
+
+	await reloadEditor( {
 		plugins: [ ArticlePluginSet, Table, RestrictedEditingException ],
-		toolbar: [ 'heading', '|',
-			'bold', 'italic', 'link', '|',
+		toolbar: [
+			'heading', '|', 'bold', 'italic', 'link', '|',
 			'bulletedList', 'numberedList', 'blockQuote', 'insertTable', '|',
 			'restrictedEditingException', '|', 'undo', 'redo'
 		],
@@ -29,10 +50,29 @@ ClassicEditor
 				'mergeTableCells'
 			]
 		}
-	} )
-	.then( editor => {
-		window.editor = editor;
-	} )
-	.catch( err => {
-		console.error( err.stack );
 	} );
+
+	currentModeDisplay.innerHTML = 'Current Mode: <span class="mode mode-standard">STANDARD</span>';
+	enableSwitchToRestrictedMode();
+}
+
+async function startRestrictedMode() {
+	restrictedModeButton.removeEventListener( 'click', startRestrictedMode );
+	restrictedModeButton.setAttribute( 'disabled', 'disabled' );
+
+	await reloadEditor( {
+		plugins: [ ArticlePluginSet, Table, RestrictedEditing ],
+		toolbar: [ 'bold', 'italic', 'link', '|', 'restrictedEditing', '|', 'undo', 'redo' ]
+	} );
+
+	currentModeDisplay.innerHTML = 'Current Mode: <span class="mode mode-restricted">RESTRICTED</span>';
+	enableSwitchToStandardMode();
+}
+
+async function reloadEditor( config ) {
+	if ( window.editor ) {
+		await window.editor.destroy();
+	}
+
+	window.editor = await ClassicEditor.create( document.querySelector( '#editor' ), config );
+}

+ 5 - 0
packages/ckeditor5-restricted-editing/tests/manual/restrictedediting.md

@@ -2,3 +2,8 @@
 
 1. The editor data should be loaded with "it is editable" fragment marked as an exception in the first paragraph (orange-ish background).
 2. Test the "Restricted editing" button to mark parts of text as non-restricted.
+
+## Restricted editing - Restricted mode
+
+1. The editor data should be loaded with the same exception fragments as defined in standard mode.
+2. The editor toolbar should have limited set of buttons for basic styles.

+ 180 - 0
packages/ckeditor5-restricted-editing/tests/restrictedediting.js

@@ -0,0 +1,180 @@
+/**
+ * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
+ */
+
+/* global document */
+
+import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
+import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
+
+import RestrictedEditing from './../src/restrictedediting';
+import { setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
+import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
+import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
+import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
+
+describe( 'RestrictedEditing', () => {
+	let editor, element;
+
+	testUtils.createSinonSandbox();
+
+	describe( 'plugin', () => {
+		beforeEach( async () => {
+			element = document.createElement( 'div' );
+			document.body.appendChild( element );
+
+			editor = await ClassicTestEditor.create( element, { plugins: [ RestrictedEditing ] } );
+		} );
+
+		afterEach( () => {
+			element.remove();
+
+			return editor.destroy();
+		} );
+
+		it( 'should be named', () => {
+			expect( RestrictedEditing.pluginName ).to.equal( 'RestrictedEditing' );
+		} );
+
+		it( 'should be loaded', () => {
+			expect( editor.plugins.get( RestrictedEditing ) ).to.be.instanceOf( RestrictedEditing );
+		} );
+	} );
+
+	describe( 'conversion', () => {
+		let model;
+
+		beforeEach( async () => {
+			editor = await VirtualTestEditor.create( { plugins: [ Paragraph, RestrictedEditing ] } );
+			model = editor.model;
+		} );
+
+		afterEach( () => {
+			return editor.destroy();
+		} );
+
+		describe( 'upcast', () => {
+			it( 'should convert <span class="ck-restricted-editing-exception"> to marker', () => {
+				editor.setData( '<p>foo <span class="ck-restricted-editing-exception">bar</span> baz</p>' );
+
+				expect( model.markers.has( 'restricted-editing-exception:1' ) ).to.be.true;
+
+				const marker = model.markers.get( 'restricted-editing-exception:1' );
+
+				expect( marker.getStart().path ).to.deep.equal( [ 0, 4 ] );
+				expect( marker.getEnd().path ).to.deep.equal( [ 0, 7 ] );
+			} );
+
+			it( 'should convert multiple <span class="ck-restricted-editing-exception">', () => {
+				editor.setData(
+					'<p>foo <span class="ck-restricted-editing-exception">bar</span> baz</p>' +
+					'<p>ABCDEF<span class="ck-restricted-editing-exception">GHIJK</span>LMNOPQRST</p>'
+				);
+
+				expect( model.markers.has( 'restricted-editing-exception:1' ) ).to.be.true;
+				expect( model.markers.has( 'restricted-editing-exception:2' ) ).to.be.true;
+
+				// Data for the first marker is the same as in previous tests so no need to test it again.
+				const secondMarker = model.markers.get( 'restricted-editing-exception:2' );
+
+				expect( secondMarker.getStart().path ).to.deep.equal( [ 1, 6 ] );
+				expect( secondMarker.getEnd().path ).to.deep.equal( [ 1, 11 ] );
+			} );
+
+			it( 'should not convert other <span> elements', () => {
+				editor.setData( '<p>foo <span class="foo bar">bar</span> baz</p>' );
+
+				expect( model.markers.has( 'restricted-editing-exception:1' ) ).to.be.false;
+			} );
+		} );
+
+		describe( 'downcast', () => {
+			it( 'should convert model marker to <span>', () => {
+				setModelData( model, '<paragraph>foo bar baz</paragraph>' );
+
+				const paragraph = model.document.getRoot().getChild( 0 );
+
+				model.change( writer => {
+					writer.addMarker( 'restricted-editing-exception:1', {
+						range: writer.createRange( writer.createPositionAt( paragraph, 4 ), writer.createPositionAt( paragraph, 7 ) ),
+						usingOperation: true,
+						affectsData: true
+					} );
+				} );
+
+				const expectedView = '<p>foo <span class="ck-restricted-editing-exception">bar</span> baz</p>';
+				expect( editor.getData() ).to.equal( expectedView );
+				expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( expectedView );
+			} );
+
+			it( 'converted <span> should be the outermost attribute element', () => {
+				editor.conversion.for( 'downcast' ).attributeToElement( { model: 'bold', view: 'b' } );
+				setModelData( model, '<paragraph><$text bold="true">foo bar baz</$text></paragraph>' );
+
+				const paragraph = model.document.getRoot().getChild( 0 );
+
+				model.change( writer => {
+					writer.addMarker( 'restricted-editing-exception:1', {
+						range: writer.createRange( writer.createPositionAt( paragraph, 0 ), writer.createPositionAt( paragraph, 'end' ) ),
+						usingOperation: true,
+						affectsData: true
+					} );
+				} );
+
+				const expectedView = '<p><span class="ck-restricted-editing-exception"><b>foo bar baz</b></span></p>';
+				expect( editor.getData() ).to.equal( expectedView );
+				expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( expectedView );
+			} );
+		} );
+	} );
+
+	describe( 'editing behavior', () => {
+		let model;
+
+		beforeEach( async () => {
+			editor = await VirtualTestEditor.create( { plugins: [ Paragraph, RestrictedEditing ] } );
+			model = editor.model;
+		} );
+
+		afterEach( () => {
+			return editor.destroy();
+		} );
+
+		it( 'should keep markers in the view when editable region is edited', () => {
+			setModelData( model,
+				'<paragraph>foo bar baz</paragraph>' +
+				'<paragraph>xxx y[]yy zzz</paragraph>'
+			);
+
+			const firstParagraph = model.document.getRoot().getChild( 0 );
+			const secondParagraph = model.document.getRoot().getChild( 1 );
+
+			model.change( writer => {
+				writer.addMarker( 'restricted-editing-exception:1', {
+					range: writer.createRange( writer.createPositionAt( firstParagraph, 4 ), writer.createPositionAt( firstParagraph, 7 ) ),
+					usingOperation: true,
+					affectsData: true
+				} );
+				writer.addMarker( 'restricted-editing-exception:2', {
+					range: writer.createRange(
+						writer.createPositionAt( secondParagraph, 4 ),
+						writer.createPositionAt( secondParagraph, 7 )
+					),
+					usingOperation: true,
+					affectsData: true
+				} );
+			} );
+
+			model.change( writer => {
+				model.insertContent( writer.createText( 'R', model.document.selection.getAttributes() ) );
+			} );
+
+			const expectedView = '<p>foo <span class="ck-restricted-editing-exception">bar</span> baz</p>' +
+				'<p>xxx <span class="ck-restricted-editing-exception">yRyy</span> zzz</p>';
+
+			expect( editor.getData() ).to.equal( expectedView );
+			expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( expectedView );
+		} );
+	} );
+} );

+ 26 - 0
packages/ckeditor5-restricted-editing/tests/restrictededitingexceptionediting.js

@@ -11,6 +11,7 @@ import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
 
 import RestrictedEditingExceptionEditing from '../src/restrictededitingexceptionediting';
 import RestrictedEditingExceptionCommand from '../src/restrictededitingexceptioncommand';
+import { assertEqualMarkup } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
 
 describe( 'RestrictedEditingExceptionEditing', () => {
 	let editor, model;
@@ -70,6 +71,31 @@ describe( 'RestrictedEditingExceptionEditing', () => {
 				expect( editor.getData() ).to.equal( expectedView );
 				expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( expectedView );
 			} );
+
+			it( 'converted <span> should be outer most element', () => {
+				editor.conversion.for( 'downcast' ).attributeToElement( {
+					model: 'bold',
+					view: 'b'
+				} );
+				editor.conversion.for( 'downcast' ).attributeToElement( {
+					model: 'italic',
+					view: 'i'
+				} );
+
+				const expectedView = '<p><span class="ck-restricted-editing-exception"><b>foo</b> <i>bar</i> baz</span></p>';
+
+				setModelData( editor.model,
+					'<paragraph>' +
+						'<$text restrictedEditingException="true" bold="true">foo</$text>' +
+						'<$text restrictedEditingException="true"> </$text>' +
+						'<$text restrictedEditingException="true" italic="true">bar</$text>' +
+						'<$text restrictedEditingException="true"> baz</$text>' +
+					'</paragraph>'
+				);
+
+				assertEqualMarkup( editor.getData(), expectedView );
+				assertEqualMarkup( getViewData( editor.editing.view, { withoutSelection: true } ), expectedView );
+			} );
 		} );
 	} );
 } );