This repository has been archived by the owner on Jun 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 40
Introduced schema#setAttributeProperties() and schema#getAttributeProperties() methods #1711
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7cd8a43
Feature: Introduced schema#setAttributeProperties() and schema#getAtt…
mlewand 5b0ce2d
Docs: Improved docs for setAttributeProperties and added a custom typ…
mlewand 72604d5
Code style: Moved schema#getAttributeProperties and schema#setAttribu…
mlewand 0495b38
Docs: Fixed API reference link.
mlewand 118b34c
Tests: Simplified common code.
mlewand 9983483
Internal: Applied review corrections. [skip ci]
mlewand 99764b9
Code style: Restored getDefinitions to its rightful place. [skip ci]
mlewand 4688a17
Docs: More explicit information that it's acceptable to use other att…
mlewand File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,6 +39,14 @@ export default class Schema { | |
constructor() { | ||
this._sourceDefinitions = {}; | ||
|
||
/** | ||
* A dictionary containing attribute properties. | ||
* | ||
* @private | ||
* @member {Object.<String,String>} | ||
*/ | ||
this._attributeProperties = {}; | ||
|
||
this.decorate( 'checkChild' ); | ||
this.decorate( 'checkAttribute' ); | ||
|
||
|
@@ -475,6 +483,58 @@ export default class Schema { | |
}, { priority: 'high' } ); | ||
} | ||
|
||
/** | ||
* This method allows assigning additional metadata to the model attributes. For example, | ||
* {@link module:engine/model/schema~AttributeProperties `AttributeProperties#isFormatting` property} is | ||
* used to mark formatting attributes (like `bold` or `italic`). | ||
* | ||
* // Mark bold as a formatting attribute. | ||
* schema.setAttributeProperties( 'bold', { | ||
* isFormatting: true | ||
* } ); | ||
* | ||
* // Override code not to be considered a formatting markup. | ||
* schema.setAttributeProperties( 'code', { | ||
* isFormatting: false | ||
* } ); | ||
* | ||
* Properties are not limited to members defined in the | ||
* {@link module:engine/model/schema~AttributeProperties `AttributeProperties` type} and you can also use custom properties: | ||
* | ||
* schema.setAttributeProperties( 'blockQuote', { | ||
* customProperty: 'value' | ||
* } ); | ||
* | ||
* Subsequent calls with the same attribute will extend its custom properties: | ||
* | ||
* schema.setAttributeProperties( 'blockQuote', { | ||
* one: 1 | ||
* } ); | ||
* | ||
* schema.setAttributeProperties( 'blockQuote', { | ||
* two: 2 | ||
* } ); | ||
* | ||
* console.log( schema.getAttributeProperties( 'blockQuote' ) ); | ||
* // Logs: { one: 1, two: 2 } | ||
* | ||
* @param {String} attributeName A name of the attribute to receive the properties. | ||
* @param {module:engine/model/schema~AttributeProperties} properties A dictionary of properties. | ||
*/ | ||
setAttributeProperties( attributeName, properties ) { | ||
this._attributeProperties[ attributeName ] = Object.assign( this.getAttributeProperties( attributeName ) || {}, properties ); | ||
} | ||
|
||
/** | ||
* Returns properties associated with a given model attribute. See {@link #setAttributeProperties `setAttributeProperties()`}. | ||
* | ||
* @param {String} attributeName A name of the attribute. | ||
* @returns {module:engine/model/schema~AttributeProperties} | ||
*/ | ||
getAttributeProperties( attributeName ) { | ||
return this._attributeProperties[ attributeName ]; | ||
} | ||
|
||
/** | ||
* Returns the lowest {@link module:engine/model/schema~Schema#isLimit limit element} containing the entire | ||
* selection/range/position or the root otherwise. | ||
|
@@ -1285,6 +1345,16 @@ export class SchemaContext { | |
* @typedef {Object} module:engine/model/schema~SchemaContextItem | ||
*/ | ||
|
||
/** | ||
* A structure containing additional metadata describing the attribute. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As I wrote earlier, |
||
* | ||
* See {@link module:engine/model/schema~Schema#setAttributeProperties `Schema#setAttributeProperties()`} for usage examples. | ||
* | ||
* @typedef {Object} module:engine/model/schema~AttributeProperties | ||
* @property {Boolean} [isFormatting] Indicates that the attribute should be considered as a visual formatting, like `bold`, `italic` or | ||
* `fontSize` rather than semantic attribute (such as `src`, `listType`, etc.). For example, it is used by the "Remove format" feature. | ||
*/ | ||
|
||
function compileBaseItemRule( sourceItemRules, itemName ) { | ||
const itemRule = { | ||
name: itemName, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is confusing. First, it is stated that
Then we enforce a type, which clearly declares which properties are allowed.