-
Notifications
You must be signed in to change notification settings - Fork 152
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add prompt button, remove execCommand for links
fixes #98
- Loading branch information
Showing
23 changed files
with
403 additions
and
420 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,36 +1,10 @@ | ||
import TextFormatCommand from './text-format'; | ||
import { | ||
any | ||
} from '../utils/array-utils'; | ||
|
||
export default class BoldCommand extends TextFormatCommand { | ||
constructor(editor) { | ||
super({ | ||
super(editor, { | ||
tag: 'strong', | ||
name: 'bold', | ||
button: '<i class="ck-icon-bold"></i>' | ||
}); | ||
this.editor = editor; | ||
const { builder } = this.editor; | ||
this.markup = builder.createMarkup('strong'); | ||
} | ||
exec() { | ||
let markerRange = this.editor.cursor.offsets; | ||
if (!markerRange.headSection || !markerRange.tailSection) { | ||
return; | ||
} | ||
let markers = this.editor.run((postEditor) => { | ||
return postEditor.applyMarkupToMarkers(markerRange, this.markup); | ||
}); | ||
this.editor.selectMarkers(markers); | ||
} | ||
unexec() { | ||
let markerRange = this.editor.cursor.offsets; | ||
let markers = this.editor.run((postEditor) => { | ||
return postEditor.removeMarkupFromMarkers(markerRange, this.markup); | ||
}); | ||
this.editor.selectMarkers(markers); | ||
} | ||
isActive() { | ||
return any(this.editor.activeMarkers, m => m.hasMarkup(this.markup)); | ||
} | ||
} |
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,36 +1,11 @@ | ||
import TextFormatCommand from './text-format'; | ||
import { | ||
any | ||
} from '../utils/array-utils'; | ||
|
||
export default class ItalicCommand extends TextFormatCommand { | ||
constructor(editor) { | ||
super({ | ||
super(editor, { | ||
tag: 'em', | ||
name: 'italic', | ||
button: '<i class="ck-icon-italic"></i>' | ||
}); | ||
this.editor = editor; | ||
const { builder } = this.editor; | ||
this.markup = builder.createMarkup('em'); | ||
} | ||
exec() { | ||
let markerRange = this.editor.cursor.offsets; | ||
if (!markerRange.headSection || !markerRange.tailSection) { | ||
return; | ||
} | ||
let markers = this.editor.run((postEditor) => { | ||
return postEditor.applyMarkupToMarkers(markerRange, this.markup); | ||
}); | ||
this.editor.selectMarkers(markers); | ||
} | ||
unexec() { | ||
let markerRange = this.editor.cursor.offsets; | ||
let markers = this.editor.run((postEditor) => { | ||
return postEditor.removeMarkupFromMarkers(markerRange, this.markup); | ||
}); | ||
this.editor.selectMarkers(markers); | ||
} | ||
isActive() { | ||
return any(this.editor.activeMarkers, m => m.hasMarkup(this.markup)); | ||
} | ||
} |
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,38 +1,46 @@ | ||
import TextFormatCommand from './text-format'; | ||
import Prompt from '../views/prompt'; | ||
import { getSelectionTagName } from '../utils/selection-utils'; | ||
import { inherit } from 'content-kit-utils'; | ||
|
||
var RegExpHttp = /^https?:\/\//i; | ||
|
||
function LinkCommand() { | ||
TextFormatCommand.call(this, { | ||
name: 'link', | ||
tag: 'a', | ||
action: 'createLink', | ||
removeAction: 'unlink', | ||
button: '<i class="ck-icon-link"></i>', | ||
prompt: new Prompt({ | ||
command: this, | ||
placeholder: 'Enter a url, press return...' | ||
}) | ||
}); | ||
} | ||
inherit(LinkCommand, TextFormatCommand); | ||
import { any } from 'content-kit-editor/utils/array-utils'; | ||
|
||
export default class LinkCommand extends TextFormatCommand { | ||
constructor(editor) { | ||
super(editor, { | ||
name: 'link', | ||
tag: 'a', | ||
button: '<i class="ck-icon-link"></i>' | ||
}); | ||
} | ||
|
||
LinkCommand.prototype.exec = function(url) { | ||
if (!url) { | ||
return LinkCommand._super.prototype.unexec.call(this); | ||
isActive() { | ||
return any(this.editor.activeMarkers, m => m.hasMarkup(this.tag)); | ||
} | ||
|
||
if(this.tag === getSelectionTagName()) { | ||
this.unexec(); | ||
} else { | ||
if (!RegExpHttp.test(url)) { | ||
url = 'http://' + url; | ||
} | ||
LinkCommand._super.prototype.exec.call(this, url); | ||
exec(url) { | ||
const range = this.editor.cursor.offsets; | ||
|
||
let markers = this.editor.run(postEditor => { | ||
const markup = postEditor.builder.createMarkup('a', ['href', url]); | ||
return postEditor.applyMarkupToMarkers(range, markup); | ||
}); | ||
|
||
if (markers.length) { | ||
let lastMarker = markers[markers.length - 1]; | ||
this.editor.cursor.moveToMarker(lastMarker, lastMarker.length); | ||
} /* else { | ||
// FIXME should handle the case when linking creating no new markers | ||
// this.editor.cursor.moveToSection(range.head.section); | ||
} */ | ||
} | ||
}; | ||
|
||
export default LinkCommand; | ||
unexec() { | ||
const range = this.editor.cursor.offsets; | ||
|
||
const markers = this.editor.run(postEditor => { | ||
return postEditor.removeMarkupFromMarkers( | ||
range, | ||
markup => markup.hasTag('a') | ||
); | ||
}); | ||
|
||
this.editor.selectMarkers(markers); | ||
} | ||
} |
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,23 +1,37 @@ | ||
import Command from './base'; | ||
import { any } from '../utils/array-utils'; | ||
|
||
export default class TextFormatCommand extends Command { | ||
constructor(options={}) { | ||
constructor(editor, options={}) { | ||
super(options); | ||
|
||
this.editor = editor; | ||
this.tag = options.tag; | ||
this.mappedTags = options.mappedTags || []; | ||
if (this.tag) { | ||
this.mappedTags.push(this.tag); | ||
} | ||
this.action = options.action || this.name; | ||
this.removeAction = options.removeAction || this.action; | ||
} | ||
|
||
exec(value) { | ||
document.execCommand(this.action, false, value || null); | ||
get markup() { | ||
if (this._markup) { return this._markup; } | ||
const { builder } = this.editor; | ||
this._markup = builder.createMarkup(this.tag); | ||
return this._markup; | ||
} | ||
|
||
isActive() { | ||
return any(this.editor.activeMarkers, m => m.hasMarkup(this.markup)); | ||
} | ||
|
||
exec() { | ||
const range = this.editor.cursor.offsets; | ||
const markers = this.editor.run((postEditor) => { | ||
return postEditor.applyMarkupToMarkers(range, this.markup); | ||
}); | ||
this.editor.selectMarkers(markers); | ||
} | ||
|
||
unexec(value) { | ||
document.execCommand(this.removeAction, false, value || null); | ||
unexec() { | ||
const range = this.editor.cursor.offsets; | ||
const markers = this.editor.run((postEditor) => { | ||
return postEditor.removeMarkupFromMarkers(range, this.markup); | ||
}); | ||
this.editor.selectMarkers(markers); | ||
} | ||
} |
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
Oops, something went wrong.