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

Support for "HorizontalRuleCommand#isEnabled".

Kamil Piechaczek 6 лет назад
Родитель
Сommit
25c4584822

+ 2 - 1
packages/ckeditor5-horizontal-line/src/horizontalrulecommand.js

@@ -9,13 +9,14 @@
 
 
 import Command from '@ckeditor/ckeditor5-core/src/command';
 import Command from '@ckeditor/ckeditor5-core/src/command';
 import { findOptimalInsertionPosition } from '@ckeditor/ckeditor5-widget/src/utils';
 import { findOptimalInsertionPosition } from '@ckeditor/ckeditor5-widget/src/utils';
+import { isHorizontalRuleAllowed } from './utils';
 
 
 export default class HorizontalRuleCommand extends Command {
 export default class HorizontalRuleCommand extends Command {
 	/**
 	/**
 	 * @inheritDoc
 	 * @inheritDoc
 	 */
 	 */
 	refresh() {
 	refresh() {
-		this.isEnabled = true;
+		this.isEnabled = isHorizontalRuleAllowed( this.editor.model );
 	}
 	}
 
 
 	/**
 	/**

+ 2 - 5
packages/ckeditor5-horizontal-line/src/horizontalruleediting.js

@@ -8,8 +8,8 @@
  */
  */
 
 
 import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
 import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
-import { toWidget } from '@ckeditor/ckeditor5-widget/src/utils';
 import HorizontalRuleCommand from './horizontalrulecommand';
 import HorizontalRuleCommand from './horizontalrulecommand';
+import { toHorizontalRuleWidget } from './utils';
 
 
 import '../theme/horizontalrule.css';
 import '../theme/horizontalrule.css';
 
 
@@ -48,10 +48,7 @@ export default class HorizontalRuleEditing extends Plugin {
 				viewWriter.addClass( 'ck-horizontal-rule', viewElement );
 				viewWriter.addClass( 'ck-horizontal-rule', viewElement );
 				viewWriter.setCustomProperty( 'hr', true, viewElement );
 				viewWriter.setCustomProperty( 'hr', true, viewElement );
 
 
-				return toWidget( viewElement, viewWriter, {
-					label,
-					hasSelectionHandler: true
-				} );
+				return toHorizontalRuleWidget( viewElement, viewWriter, label );
 			}
 			}
 		} );
 		} );
 
 

+ 86 - 0
packages/ckeditor5-horizontal-line/src/utils.js

@@ -0,0 +1,86 @@
+/**
+ * @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 horizontal-rule/utils
+ */
+
+import { findOptimalInsertionPosition, isWidget, toWidget } from '@ckeditor/ckeditor5-widget/src/utils';
+
+/**
+ * @param {module:engine/view/element~Element} viewElement
+ * @param {module:engine/view/downcastwriter~DowncastWriter} writer An instance of the view writer.
+ * @param {String} label The element's label.
+ * @returns {module:engine/view/element~Element}
+ */
+export function toHorizontalRuleWidget( viewElement, writer, label ) {
+	writer.setCustomProperty( 'horizontalRule', true, viewElement );
+
+	return toWidget( viewElement, writer, { label, hasSelectionHandler: true } );
+}
+
+/**
+ * Checks if a given view element is a horizontal rule widget.
+ *
+ * @param {module:engine/view/element~Element} viewElement
+ * @returns {Boolean}
+ */
+export function isHorizontalRuleWidget( viewElement ) {
+	return !!viewElement.getCustomProperty( 'horizontalRule' ) && isWidget( viewElement );
+}
+
+/**
+ * Checks if the `horizontalRule` element can be inserted at current model selection.
+ *
+ * @param {module:engine/model/model~Model} model
+ * @returns {Boolean}
+ */
+export function isHorizontalRuleAllowed( model ) {
+	const schema = model.schema;
+	const selection = model.document.selection;
+
+	return isHorizontalRuleAllowedInParent( selection, schema, model ) &&
+		!checkSelectionOnObject( selection, schema );
+}
+
+// Checks if horizontal rule is allowed by schema in optimal insertion parent.
+//
+// @param {module:engine/model/selection~Selection|module:engine/model/documentselection~DocumentSelection} selection
+// @param {module:engine/model/schema~Schema} schema
+// @param {module:engine/model/model~Model} model Model instance.
+// @returns {Boolean}
+function isHorizontalRuleAllowedInParent( selection, schema, model ) {
+	const parent = getInsertHorizontalRuleParent( selection, model );
+
+	return schema.checkChild( parent, 'horizontalRule' );
+}
+
+// Check if selection is on object.
+//
+// @param {module:engine/model/selection~Selection|module:engine/model/documentselection~DocumentSelection} selection
+// @param {module:engine/model/schema~Schema} schema
+// @returns {Boolean}
+function checkSelectionOnObject( selection, schema ) {
+	const selectedElement = selection.getSelectedElement();
+
+	return selectedElement && schema.isObject( selectedElement );
+}
+
+// Returns a node that will be used to insert horizontal rule with `model.insertContent` to check if horizontal rule can be placed there.
+//
+// @param {module:engine/model/selection~Selection|module:engine/model/documentselection~DocumentSelection} selection
+// @param {module:engine/model/model~Model} model Model instance.
+// @returns {module:engine/model/element~Element}
+function getInsertHorizontalRuleParent( selection, model ) {
+	const insertAt = findOptimalInsertionPosition( selection, model );
+
+	const parent = insertAt.parent;
+
+	if ( parent.isEmpty && !parent.is( '$root' ) ) {
+		return parent.parent;
+	}
+
+	return parent;
+}