-
Notifications
You must be signed in to change notification settings - Fork 156
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 #32 from bustlelabs/refactor-editor
Refactor editor to delegate selection methods to `Cursor`
- Loading branch information
Showing
26 changed files
with
562 additions
and
372 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,47 @@ | ||
import TextFormatCommand from './text-format'; | ||
import { getSelectionBlockElement, selectNode } from '../utils/selection-utils'; | ||
import { inherit } from 'content-kit-utils'; | ||
import { | ||
any | ||
} from '../utils/array-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(editor, options={}) { | ||
super(options); | ||
this.editor = editor; | ||
} | ||
|
||
isActive() { | ||
const editor = this.editor; | ||
const activeSections = editor.activeSections; | ||
|
||
return any(activeSections, section => { | ||
return any(this.mappedTags, t => section.tagName === t); | ||
}); | ||
} | ||
|
||
exec() { | ||
const editor = this.editor; | ||
const activeSections = editor.activeSections; | ||
|
||
activeSections.forEach(s => { | ||
editor.resetSectionMarkers(s); | ||
editor.setSectionTagName(s, this.tag); | ||
}); | ||
|
||
editor.rerender(); | ||
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.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(editor) { | ||
const options = { | ||
name: 'heading', | ||
tag: 'h2', | ||
button: '<i class="ck-icon-heading"></i>2' | ||
}; | ||
super(editor, 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,13 +1,11 @@ | ||
import FormatBlockCommand from './format-block'; | ||
import { inherit } from 'content-kit-utils'; | ||
|
||
function QuoteCommand() { | ||
FormatBlockCommand.call(this, { | ||
name: 'quote', | ||
tag: 'blockquote', | ||
button: '<i class="ck-icon-quote"></i>' | ||
}); | ||
export default class QuoteCommand extends FormatBlockCommand { | ||
constructor(editor) { | ||
super(editor, { | ||
name: 'quote', | ||
tag: 'blockquote', | ||
button: '<i class="ck-icon-quote"></i>' | ||
}); | ||
} | ||
} | ||
inherit(QuoteCommand, FormatBlockCommand); | ||
|
||
export default QuoteCommand; |
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,11 @@ | ||
import FormatBlockCommand from './format-block'; | ||
import { inherit } from 'content-kit-utils'; | ||
|
||
function SubheadingCommand() { | ||
FormatBlockCommand.call(this, { | ||
name: 'subheading', | ||
tag: 'h3', | ||
button: '<i class="ck-icon-heading"></i>2' | ||
}); | ||
export default class SubheadingCommand extends FormatBlockCommand { | ||
constructor(editor) { | ||
super(editor, { | ||
name: 'subheading', | ||
tag: 'h3', | ||
button: '<i class="ck-icon-heading"></i>3' | ||
}); | ||
} | ||
} | ||
inherit(SubheadingCommand, FormatBlockCommand); | ||
|
||
export default SubheadingCommand; |
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); | ||
} | ||
} |
Oops, something went wrong.