-
Notifications
You must be signed in to change notification settings - Fork 151
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Classify commands, change FormatBlock command to operate semantically
* Add tests for showing/hiding toolbar and selection for heading
- Loading branch information
Showing
18 changed files
with
340 additions
and
154 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,13 +1,13 @@ | ||
function Command(options) { | ||
options = options || {}; | ||
var command = this; | ||
var name = options.name; | ||
var prompt = options.prompt; | ||
command.name = name; | ||
command.button = options.button || name; | ||
if (prompt) { command.prompt = prompt; } | ||
} | ||
|
||
Command.prototype.exec = function() {}; | ||
export default class Command { | ||
constructor(options={}) { | ||
var command = this; | ||
var name = options.name; | ||
var prompt = options.prompt; | ||
command.name = name; | ||
command.button = options.button || name; | ||
if (prompt) { command.prompt = prompt; } | ||
} | ||
|
||
export default Command; | ||
exec() {/* override in subclass */} | ||
unexec() {/* override in subclass */} | ||
} |
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,28 +1,24 @@ | ||
import Command from './base'; | ||
import { inherit } from 'content-kit-utils'; | ||
|
||
function injectCardBlock(/* cardName, cardPayload, editor, index */) { | ||
throw new Error('Unimplemented: BlockModel and Type.CARD are no longer things'); | ||
} | ||
|
||
function CardCommand() { | ||
Command.call(this, { | ||
name: 'card', | ||
button: '<i>CA</i>' | ||
}); | ||
} | ||
inherit(CardCommand, Command); | ||
export default class CardCommand extends Command { | ||
constructor() { | ||
super({ | ||
name: 'card', | ||
button: '<i>CA</i>' | ||
}); | ||
} | ||
|
||
CardCommand.prototype = { | ||
exec: function() { | ||
CardCommand._super.prototype.exec.call(this); | ||
var editor = this.editorContext; | ||
var currentEditingIndex = editor.getCurrentBlockIndex(); | ||
exec() { | ||
super.exec(); | ||
const editor = this.editor; | ||
const currentEditingIndex = editor.getCurrentBlockIndex(); | ||
|
||
var cardName = 'pick-color'; | ||
var cardPayload = { options: ['red', 'blue'] }; | ||
const cardName = 'pick-color'; | ||
const cardPayload = { options: ['red', 'blue'] }; | ||
injectCardBlock(cardName, cardPayload, editor, currentEditingIndex); | ||
} | ||
}; | ||
|
||
export default CardCommand; | ||
} |
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,36 +1,38 @@ | ||
import TextFormatCommand from './text-format'; | ||
import { getSelectionBlockElement, selectNode } from '../utils/selection-utils'; | ||
import { inherit } from 'content-kit-utils'; | ||
|
||
function FormatBlockCommand(options) { | ||
options = options || {}; | ||
options.action = 'formatBlock'; | ||
TextFormatCommand.call(this, options); | ||
} | ||
inherit(FormatBlockCommand, TextFormatCommand); | ||
|
||
FormatBlockCommand.prototype.exec = function() { | ||
var tag = this.tag; | ||
// Brackets neccessary for certain browsers | ||
var value = '<' + tag + '>'; | ||
var blockElement = getSelectionBlockElement(); | ||
// Allow block commands to be toggled back to a text block | ||
if(tag === blockElement.tagName.toLowerCase()) { | ||
throw new Error('Unimplemented: Type.BOLD.paragraph must be replaced'); | ||
/* | ||
value = Type.PARAGRAPH.tag; | ||
*/ | ||
} else { | ||
// Flattens the selection before applying the block format. | ||
// Otherwise, undesirable nested blocks can occur. | ||
// TODO: would love to be able to remove this | ||
var flatNode = document.createTextNode(blockElement.textContent); | ||
blockElement.parentNode.insertBefore(flatNode, blockElement); | ||
blockElement.parentNode.removeChild(blockElement); | ||
selectNode(flatNode); | ||
class FormatBlockCommand extends TextFormatCommand { | ||
constructor(options={}) { | ||
super(options); | ||
} | ||
|
||
exec() { | ||
const editor = this.editor; | ||
const activeSections = editor.activeSections; | ||
|
||
activeSections.forEach(s => { | ||
editor.resetSectionMarkers(s); | ||
editor.setSectionTagName(s, this.tag); | ||
}); | ||
|
||
editor.rerender(); | ||
editor.trigger('update'); // FIXME -- should be handled by editor | ||
|
||
editor.selectSections(activeSections); | ||
} | ||
|
||
FormatBlockCommand._super.prototype.exec.call(this, value); | ||
}; | ||
unexec() { | ||
const editor = this.editor; | ||
const activeSections = editor.activeSections; | ||
|
||
activeSections.forEach(s => { | ||
editor.resetSectionTagName(s); | ||
}); | ||
|
||
editor.rerender(); | ||
editor.trigger('update'); // FIXME -- should be handled by editor | ||
|
||
editor.selectSections(activeSections); | ||
} | ||
} | ||
|
||
export default FormatBlockCommand; |
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,13 +1,12 @@ | ||
import FormatBlockCommand from './format-block'; | ||
import { inherit } from 'content-kit-utils'; | ||
|
||
function HeadingCommand() { | ||
FormatBlockCommand.call(this, { | ||
name: 'heading', | ||
tag: 'h2', | ||
button: '<i class="ck-icon-heading"></i>1' | ||
}); | ||
export default class HeadingCommand extends FormatBlockCommand { | ||
constructor() { | ||
const options = { | ||
name: 'heading', | ||
tag: 'h2', | ||
button: '<i class="ck-icon-heading"></i>1' | ||
}; | ||
super(options); | ||
} | ||
} | ||
inherit(HeadingCommand, FormatBlockCommand); | ||
|
||
export default HeadingCommand; |
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 |
---|---|---|
@@ -1,24 +1,21 @@ | ||
import Command from './base'; | ||
import { inherit } from 'content-kit-utils'; | ||
|
||
function TextFormatCommand(options) { | ||
options = options || {}; | ||
Command.call(this, options); | ||
this.tag = options.tag; | ||
this.mappedTags = options.mappedTags || []; | ||
this.mappedTags.push(this.tag); | ||
this.action = options.action || this.name; | ||
this.removeAction = options.removeAction || this.action; | ||
} | ||
inherit(TextFormatCommand, Command); | ||
export default class TextFormatCommand extends Command { | ||
constructor(options={}) { | ||
super(options); | ||
|
||
this.tag = options.tag; | ||
this.mappedTags = options.mappedTags || []; | ||
this.mappedTags.push(this.tag); | ||
this.action = options.action || this.name; | ||
this.removeAction = options.removeAction || this.action; | ||
} | ||
|
||
TextFormatCommand.prototype = { | ||
exec: function(value) { | ||
exec(value) { | ||
document.execCommand(this.action, false, value || null); | ||
}, | ||
unexec: function(value) { | ||
document.execCommand(this.removeAction, false, value || null); | ||
} | ||
}; | ||
|
||
export default TextFormatCommand; | ||
unexec(value) { | ||
document.execCommand(this.removeAction, false, value || null); | ||
} | ||
} |
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
Oops, something went wrong.