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

Added data pipeline v -> m conversion.

Oskar Wróbel 6 лет назад
Родитель
Сommit
f273e4a5e1

+ 25 - 0
packages/ckeditor5-list/src/todolistconverters.js

@@ -185,6 +185,31 @@ export function modelViewChangeChecked( model ) {
 	};
 }
 
+/**
+ * @see module:engine/conversion/upcastdispatcher~UpcastDispatcher#event:element
+ * @param {module:utils/eventinfo~EventInfo} evt An object containing information about the fired event.
+ * @param {Object} data An object containing conversion input and a placeholder for conversion output and possibly other values.
+ * @param {module:engine/conversion/upcastdispatcher~UpcastConversionApi} conversionApi Conversion interface to be used by the callback.
+ */
+export function dataViewModelCheckmarkInsertion( evt, data, conversionApi ) {
+	if ( data.viewItem.getAttribute( 'type' ) != 'checkbox' || data.modelCursor.parent.name != 'listItem' ) {
+		return;
+	}
+
+	if ( !conversionApi.consumable.consume( data.viewItem, { name: true } ) ) {
+		return;
+	}
+
+	const { writer } = conversionApi;
+	const modelItem = data.modelCursor.parent;
+
+	writer.setAttribute( 'listType', 'todo', modelItem );
+
+	if ( data.viewItem.getAttribute( 'checked' ) == 'checked' ) {
+		writer.setAttribute( 'todoListChecked', true, modelItem );
+	}
+}
+
 function addTodoElementsToListItem( modelItem, viewItem, viewWriter, model ) {
 	const isChecked = !!modelItem.getAttribute( 'todoListChecked' );
 

+ 4 - 1
packages/ckeditor5-list/src/todolistediting.js

@@ -18,7 +18,8 @@ import {
 	dataModelViewInsertion,
 	dataModelViewTextInsertion,
 	modelViewChangeChecked,
-	modelViewChangeType
+	modelViewChangeType,
+	dataViewModelCheckmarkInsertion
 } from './todolistconverters';
 
 /**
@@ -53,6 +54,8 @@ export default class TodoListEditing extends Plugin {
 		editing.downcastDispatcher.on( 'attribute:listType:listItem', modelViewChangeType( model ) );
 		editing.downcastDispatcher.on( 'attribute:todoListChecked:listItem', modelViewChangeChecked( model ) );
 
+		data.upcastDispatcher.on( 'element:input', dataViewModelCheckmarkInsertion, { priority: 'high' } );
+
 		// Register command for todo list.
 		editor.commands.add( 'todoList', new ListCommand( editor, 'todo' ) );