-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from regniets/feature/blockstyles
Feature/blockstyles
- Loading branch information
Showing
13 changed files
with
952 additions
and
42,805 deletions.
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
.idea | ||
node_modules | ||
yarn.lock |
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
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
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
98 changes: 79 additions & 19 deletions
98
Resources/Private/JavaScript/CkStyles/src/BlockStyleCommand.js
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 |
---|---|---|
@@ -1,35 +1,95 @@ | ||
import Command from '@ckeditor/ckeditor5-core/src/command'; | ||
// Originally taken from https://raw.githubusercontent.com/ckeditor/ckeditor5/master/packages/ckeditor5-basic-styles/src/attributecommand.js and adjusted | ||
import { Command } from 'ckeditor5-exports'; | ||
|
||
/** | ||
* Sets a class a given attribute on the top most blocks. | ||
* Set a key-value block style; e.g. "fontColor=red". | ||
*/ | ||
export default class BlockStyleCommand extends Command { | ||
|
||
export default class BlockStyleCommand extends Command { | ||
/** | ||
* @inheritDoc | ||
* @param {module:core/editor/editor~Editor} editor | ||
* @param {String} attributeKey Attribute that will be set by the command. | ||
*/ | ||
execute(options = {}) { | ||
constructor(editor, attributeKey) { | ||
super(editor); | ||
|
||
/** | ||
* The attribute that will be set by the command. | ||
* | ||
* @readonly | ||
* @member {String} | ||
*/ | ||
this.attributeKey = attributeKey; | ||
|
||
/** | ||
* Flag indicating whether the command is active. The command is active when the | ||
* {@link module:engine/model/selection~Selection#hasAttribute selection has the attribute} which means that: | ||
* | ||
* @observable | ||
* @readonly | ||
* @member {Boolean} #value | ||
*/ | ||
} | ||
|
||
/** | ||
* Updates the command's {@link #value} and {@link #isEnabled}. | ||
*/ | ||
refresh() { | ||
const model = this.editor.model; | ||
const doc = model.document; | ||
const blocksToChange = Array.from( doc.selection.getSelectedBlocks() ); | ||
|
||
const blocksToChange = getBlocksToChange( model ); | ||
this.value = this._getValueFromBlockNode(); | ||
for ( const block of blocksToChange ) { | ||
if(model.schema.checkAttribute(block, this.attributeKey)) { | ||
this.isEnabled = true; | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Executes the command — sets the attribute to the desired value. If there is no desired valued, removes the | ||
* attribute on each block. | ||
* | ||
* @fires execute | ||
* @param {Object} [options] Command options. | ||
* @param {String} [options.value] The value to be set; if null or not existing, the attribute will be removed. | ||
*/ | ||
execute(options = {}) { | ||
const model = this.editor.model; | ||
const doc = model.document; | ||
const selection = doc.selection; | ||
const value = options.value; | ||
const blocksToChange = Array.from( selection.getSelectedBlocks() ); | ||
model.change( writer => { | ||
for ( const block of blocksToChange ) { | ||
writer.setAttribute( options.key, options.value, block ); | ||
if (value) { | ||
writer.setAttribute(this.attributeKey, value, block); | ||
} else { | ||
writer.removeAttribute(this.attributeKey, block); | ||
} | ||
} | ||
} ); | ||
}); | ||
} | ||
} | ||
|
||
/** | ||
* Returns the top most blocks of the given model | ||
* | ||
* @param model | ||
* @returns {*} | ||
*/ | ||
function getBlocksToChange( model ) { | ||
const selection = model.document.selection; | ||
const schema = model.schema; | ||
return Array.from( selection.getTopMostBlocks() ); | ||
/** | ||
* Checks the attribute value of the parent block node(s) | ||
* | ||
* @private | ||
* @returns {String} The attribute value. | ||
*/ | ||
_getValueFromBlockNode() { | ||
const model = this.editor.model; | ||
const schema = model.schema; | ||
const selection = model.document.selection; | ||
const blocks = Array.from( selection.getSelectedBlocks() ); | ||
|
||
for (const block of blocks) { | ||
if (schema.checkAttribute(block, this.attributeKey)) { | ||
return block.getAttribute(this.attributeKey); | ||
} | ||
} | ||
|
||
return undefined; | ||
} | ||
} |
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
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
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
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
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 |
---|---|---|
|
@@ -57,4 +57,4 @@ export default class InlineStyleSelector extends PureComponent { | |
{ value: optionIdentifier } | ||
); | ||
} | ||
} | ||
} |
Oops, something went wrong.