Ver código fonte

Changed: Reorganized code in wrap model-to-view converter.
Docs: Fixed outdated docs.

Szymon Cofalik 8 anos atrás
pai
commit
793326487e

+ 11 - 10
packages/ckeditor5-engine/src/conversion/model-to-view-converters.js

@@ -329,18 +329,19 @@ export function wrap( elementCreator ) {
 			return;
 		}
 
-		let viewRange = conversionApi.mapper.toViewRange( data.range );
+		if ( data.item instanceof ModelSelection ) {
+			// Selection attribute conversion.
+			viewWriter.wrap( conversionApi.viewSelection.getFirstRange(), newViewElement, conversionApi.viewSelection );
+		} else {
+			// Node attribute conversion.
+			let viewRange = conversionApi.mapper.toViewRange( data.range );
 
-		// First, unwrap the range from current wrapper.
-		if ( data.attributeOldValue !== null ) {
-			viewRange = viewWriter.unwrap( viewRange, oldViewElement );
-		}
+			// First, unwrap the range from current wrapper.
+			if ( data.attributeOldValue !== null ) {
+				viewRange = viewWriter.unwrap( viewRange, oldViewElement );
+			}
 
-		// Then wrap with the new wrapper.
-		if ( data.attributeNewValue !== null ) {
-			if ( data.item instanceof ModelSelection ) {
-				viewWriter.wrap( conversionApi.viewSelection.getFirstRange(), newViewElement, conversionApi.viewSelection );
-			} else {
+			if ( data.attributeNewValue !== null ) {
 				viewWriter.wrap( viewRange, newViewElement );
 			}
 		}

+ 4 - 1
packages/ckeditor5-engine/src/conversion/modelconversiondispatcher.js

@@ -503,15 +503,18 @@ export default class ModelConversionDispatcher {
 	 * `addMarker:foo:bar` events.
 	 *
 	 * If the marker range is not collapsed:
+	 *
 	 * * the event is fired for each item in the marker range one by one,
 	 * * consumables object includes each item of the marker range and the consumable value is same as event name.
 	 *
 	 * If the marker range is collapsed:
+	 *
 	 * * there is only one event,
 	 * * consumables object includes marker range with event name.
 	 *
 	 * If selection inside a marker is converted:
-	 * * the is only one event,
+	 *
+	 * * there is only one event,
 	 * * consumables object includes selection instance with event name.
 	 *
 	 * @event addMarker

+ 3 - 4
packages/ckeditor5-engine/src/view/writer.js

@@ -456,7 +456,9 @@ export function move( sourceRange, targetPosition ) {
 
 /**
  * Wraps elements within range with provided {@link module:engine/view/attributeelement~AttributeElement AttributeElement}.
- * If a collapsed range is provided, it will be wrapped only if it is equal to view selection.
+ *
+ * If `viewSelection` was set and a collapsed range was passed, if the range is same as selection, the selection
+ * will be moved to the inside of the wrapped attribute element.
  *
  * Throws {@link module:utils/ckeditorerror~CKEditorError} `view-writer-invalid-range-container`
  * when {@link module:engine/view/range~Range#start}
@@ -465,9 +467,6 @@ export function move( sourceRange, targetPosition ) {
  * Throws {@link module:utils/ckeditorerror~CKEditorError} `view-writer-wrap-invalid-attribute` when passed attribute element is not
  * an instance of {module:engine/view/attributeelement~AttributeElement AttributeElement}.
  *
- * Throws {@link module:utils/ckeditorerror~CKEditorError} `view-writer-wrap-nonselection-collapsed-range` when passed range
- * is collapsed and different than view selection.
- *
  * @function module:engine/view/writer~writer.wrap
  * @param {module:engine/view/range~Range} range Range to wrap.
  * @param {module:engine/view/attributeelement~AttributeElement} attribute Attribute element to use as wrapper.