Explorar el Código

Tests: manual test for markers conversion integration.

Szymon Cofalik hace 9 años
padre
commit
d524da9ec4

+ 33 - 0
packages/ckeditor5-engine/tests/manual/tickets/643/1.html

@@ -0,0 +1,33 @@
+<head>
+	<link rel="stylesheet" href="%APPS_DIR%ckeditor/build/modules/amd/theme/ckeditor.css">
+	<style>
+		.h-red { display: inline-block; background: #FF0000; }
+		.h-yellow { display: inline-block; background: #FFFF00; }
+	</style>
+</head>
+
+<div id="editor">
+	<p>Lorem ipsum dolor sit amet.</p>
+	<p>Foobar.</p>
+</div>
+<br/>
+<div class="buttons">
+	<button id="add-yellow">Add yellow</button>
+	<button id="add-red">Add red</button>
+	<button id="remove-marker">Remove marker</button>
+	<br />
+	<button id="move-to-start">Move selection to start</button>
+	<button id="move-left">Move selection one char left</button>
+	<button id="move-right">Move selection one char right</button>
+</div>
+<br /><br />
+<small>
+	<em>
+		Keep in mind that this is just a simple demo.<br />
+		Highlighted markers do not merge (in model), remove button removes the first marker range which contains selection anchor.<br />
+		Once highlighted, highlight cannot be overwritten.<br />
+		Multi-element ranges are not supported<br />
+		Moving to the start button works only for flat ranges<br />
+		These are all known limitations of the demo.
+	</em>
+</small>

+ 107 - 0
packages/ckeditor5-engine/tests/manual/tickets/643/1.js

@@ -0,0 +1,107 @@
+/**
+ * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+/* globals console, window, document */
+
+import ClassicEditor from '/ckeditor5/editor-classic/classic.js';
+import Enter from '/ckeditor5/enter/enter.js';
+import Typing from '/ckeditor5/typing/typing.js';
+import Paragraph from '/ckeditor5/paragraph/paragraph.js';
+import Bold from '/ckeditor5/basic-styles/bold.js';
+import Italic from '/ckeditor5/basic-styles/italic.js';
+import List from '/ckeditor5/list/list.js';
+import Heading from '/ckeditor5/heading/heading.js';
+import Undo from '/ckeditor5/undo/undo.js';
+
+//import { wrapMarker, unwrapMarker } from '/ckeditor5/engine/conversion/model-to-view-converters.js';
+import buildModelConverter from '/ckeditor5/engine/conversion/buildmodelconverter.js';
+import Position from '/ckeditor5/engine/model/position.js';
+import LiveRange from '/ckeditor5/engine/model/liverange.js';
+import ViewAttributeElement from '/ckeditor5/engine/view/attributeelement.js';
+
+let model = null;
+
+ClassicEditor.create( document.querySelector( '#editor' ), {
+	plugins: [ Enter, Typing, Paragraph, Bold, Italic, List, Heading, Undo ],
+	toolbar: [ 'headings', 'bold', 'italic', 'bulletedList', 'numberedList', 'undo', 'redo' ]
+} )
+.then( editor => {
+	window.editor = editor;
+	model = window.editor.editing.model;
+
+	buildModelConverter().for( editor.editing.modelToView ).
+		fromMarker( 'highlight' ).
+		toElement( ( data ) => {
+			const color = data.name.split( ':' )[ 1 ];
+
+			return new ViewAttributeElement( 'span', { class: 'h-' + color } );
+		} );
+
+	window.document.getElementById( 'add-yellow' ).addEventListener( 'click', () => addHighlight( 'yellow' ) );
+	window.document.getElementById( 'add-red' ).addEventListener( 'click', () => addHighlight( 'red' ) );
+	window.document.getElementById( 'remove-marker' ).addEventListener( 'click', () => removeHighlight() );
+	window.document.getElementById( 'move-to-start' ).addEventListener( 'click', () => moveSelectionToStart() );
+	window.document.getElementById( 'move-left' ).addEventListener( 'click', () => moveSelectionByOffset( -1 ) );
+	window.document.getElementById( 'move-right' ).addEventListener( 'click', () => moveSelectionByOffset( 1 ) );
+
+	model.enqueueChanges( () => {
+		const root = model.getRoot();
+		const range = new LiveRange( new Position( root, [ 0, 10 ] ), new Position( root, [ 0, 16 ] ) );
+
+		ranges.push( range );
+		model.markers.addRange( range, 'highlight:yellow' );
+	} );
+} )
+.catch( err => {
+	console.error( err.stack );
+} );
+
+const ranges = [];
+
+function addHighlight( color ) {
+	model.enqueueChanges( () => {
+		const range = LiveRange.createFromRange( model.selection.getFirstRange() );
+
+		ranges.push( range );
+		model.markers.addRange( range, 'highlight:' + color );
+	} );
+}
+
+function removeHighlight() {
+	model.enqueueChanges( () => {
+		const pos = model.selection.getFirstPosition();
+
+		for ( let i = 0; i < ranges.length; i++ ) {
+			if ( ranges[ i ].containsPosition( pos ) || ranges[ i ].start.isEqual( pos ) || ranges[ i ].end.isEqual( pos ) ) {
+				model.markers.removeRange( ranges[ i ] );
+
+				ranges[ i ].detach();
+				ranges.splice( i, 1 );
+				break;
+			}
+		}
+	} );
+}
+
+function moveSelectionToStart() {
+	const range = model.selection.getFirstRange();
+
+	if ( range.isFlat ) {
+		model.enqueueChanges( () => {
+			model.batch().move( range, new Position( model.getRoot(), [ 0, 0 ] ) );
+		} );
+	}
+}
+
+function moveSelectionByOffset( offset ) {
+	const range = model.selection.getFirstRange();
+	const pos = offset < 0 ? range.start : range.end;
+
+	if ( range.isFlat ) {
+		model.enqueueChanges( () => {
+			model.batch().move( range, pos.getShiftedBy( offset ) );
+		} );
+	}
+}

+ 16 - 0
packages/ckeditor5-engine/tests/manual/tickets/643/1.md

@@ -0,0 +1,16 @@
+@bender-ui: collapsed
+@bender-tags: ticket, 643, iteration5
+
+### Markers integration [#643](https://github.com/ckeditor/ckeditor5-engine/issues/643)
+
+1. Write before, after and inside marked text.
+2. Remove before, after and inside marked text.
+3. Make selection intersecting with marked text and remove it.
+4. Make selection intersecting with marked text and start typing.
+5. Style text and see if marked selection brakes in some way.
+6. Click enter inside marked text.
+7. Change a paragraph with marked text to a list item or header.
+8. Remove all marked text and see if there are no artifacts.
+9. Use undo.
+10. Use buttons to create and remove markers.
+11. Make different selections and play with "move to start" button.