-
Notifications
You must be signed in to change notification settings - Fork 338
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 #169 from Voog/format_inline_rewrite
formatInline rewrite
- Loading branch information
Showing
34 changed files
with
4,269 additions
and
1,489 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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
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,16 +1,18 @@ | ||
(function(wysihtml5){ | ||
(function(wysihtml5) { | ||
|
||
var nodeOptions = { | ||
nodeName: "B", | ||
toggle: true | ||
}; | ||
|
||
wysihtml5.commands.bold = { | ||
exec: function(composer, command) { | ||
wysihtml5.commands.formatInline.execWithToggle(composer, command, "b"); | ||
wysihtml5.commands.formatInline.exec(composer, command, nodeOptions); | ||
}, | ||
|
||
state: function(composer, command) { | ||
// element.ownerDocument.queryCommandState("bold") results: | ||
// firefox: only <b> | ||
// chrome: <b>, <strong>, <h1>, <h2>, ... | ||
// ie: <b>, <strong> | ||
// opera: <b>, <strong> | ||
return wysihtml5.commands.formatInline.state(composer, command, "b"); | ||
return wysihtml5.commands.formatInline.state(composer, command, nodeOptions); | ||
} | ||
}; | ||
|
||
}(wysihtml5)); |
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,114 +1,30 @@ | ||
(function(wysihtml5) { | ||
var undef, | ||
NODE_NAME = "A", | ||
dom = wysihtml5.dom; | ||
|
||
function _format(composer, attributes) { | ||
var doc = composer.doc, | ||
tempClass = "_wysihtml5-temp-" + (+new Date()), | ||
tempClassRegExp = /non-matching-class/g, | ||
i = 0, | ||
length, | ||
anchors, | ||
anchor, | ||
hasElementChild, | ||
isEmpty, | ||
elementToSetCaretAfter, | ||
textContent, | ||
whiteSpace, | ||
j; | ||
wysihtml5.commands.formatInline.exec(composer, undef, NODE_NAME, tempClass, tempClassRegExp, undef, undef, true, true); | ||
anchors = doc.querySelectorAll(NODE_NAME + "." + tempClass); | ||
length = anchors.length; | ||
for (; i<length; i++) { | ||
anchor = anchors[i]; | ||
anchor.removeAttribute("class"); | ||
for (j in attributes) { | ||
// Do not set attribute "text" as it is meant for setting string value if created link has no textual data | ||
if (j !== "text") { | ||
anchor.setAttribute(j, attributes[j]); | ||
} | ||
} | ||
} | ||
|
||
elementToSetCaretAfter = anchor; | ||
if (length === 1) { | ||
textContent = dom.getTextContent(anchor); | ||
hasElementChild = !!anchor.querySelector("*"); | ||
isEmpty = textContent === "" || textContent === wysihtml5.INVISIBLE_SPACE; | ||
if (!hasElementChild && isEmpty) { | ||
dom.setTextContent(anchor, attributes.text || anchor.href); | ||
whiteSpace = doc.createTextNode(" "); | ||
composer.selection.setAfter(anchor); | ||
dom.insert(whiteSpace).after(anchor); | ||
elementToSetCaretAfter = whiteSpace; | ||
} | ||
} | ||
composer.selection.setAfter(elementToSetCaretAfter); | ||
} | ||
|
||
// Changes attributes of links | ||
function _changeLinks(composer, anchors, attributes) { | ||
var oldAttrs; | ||
for (var a = anchors.length; a--;) { | ||
|
||
// Remove all old attributes | ||
oldAttrs = anchors[a].attributes; | ||
for (var oa = oldAttrs.length; oa--;) { | ||
anchors[a].removeAttribute(oldAttrs.item(oa).name); | ||
} | ||
|
||
// Set new attributes | ||
for (var j in attributes) { | ||
if (attributes.hasOwnProperty(j)) { | ||
anchors[a].setAttribute(j, attributes[j]); | ||
} | ||
} | ||
var nodeOptions = { | ||
nodeName: "A", | ||
toggle: false | ||
}; | ||
|
||
} | ||
function getOptions(value) { | ||
var options = typeof value === 'object' ? value : {'href': value}; | ||
return wysihtml5.lang.object({}).merge(nodeOptions).merge({'attribute': value}).get(); | ||
} | ||
|
||
wysihtml5.commands.createLink = { | ||
/** | ||
* TODO: Use HTMLApplier or formatInline here | ||
* | ||
* Turns selection into a link | ||
* If selection is already a link, it just changes the attributes | ||
* | ||
* @example | ||
* // either ... | ||
* wysihtml5.commands.createLink.exec(composer, "createLink", "http://www.google.de"); | ||
* // ... or ... | ||
* wysihtml5.commands.createLink.exec(composer, "createLink", { href: "http://www.google.de", target: "_blank" }); | ||
*/ | ||
wysihtml5.commands.createLink = { | ||
exec: function(composer, command, value) { | ||
var anchors = this.state(composer, command); | ||
if (anchors) { | ||
// remove <a> tag if there's no attributes provided. | ||
if ((!value || !value.href) && anchors.length !== null && anchors.length !== undefined && anchors.length > 0) | ||
{ | ||
for(var i=0; i < anchors.length; i++) | ||
{ | ||
wysihtml5.dom.unwrap(anchors[i]); | ||
} | ||
return; | ||
} | ||
var opts = getOptions(value); | ||
|
||
// Selection contains links then change attributes of these links | ||
composer.selection.executeAndRestore(function() { | ||
_changeLinks(composer, anchors, value); | ||
}); | ||
} else { | ||
// Create links | ||
if (value && value.href) { | ||
value = typeof(value) === "object" ? value : { href: value }; | ||
_format(composer, value); | ||
} | ||
if (composer.selection.isCollapsed() && !this.state(composer, command)) { | ||
var textNode = composer.doc.createTextNode(opts.attribute.href); | ||
composer.selection.insertNode(textNode); | ||
composer.selection.selectNode(textNode); | ||
} | ||
wysihtml5.commands.formatInline.exec(composer, command, opts); | ||
}, | ||
|
||
state: function(composer, command) { | ||
return wysihtml5.commands.formatInline.state(composer, command, "a"); | ||
return wysihtml5.commands.formatInline.state(composer, command, nodeOptions); | ||
} | ||
}; | ||
|
||
})(wysihtml5); |
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,18 +1,16 @@ | ||
/** | ||
* document.execCommand("fontSize") will create either inline styles (firefox, chrome) or use font tags | ||
* which we don't want | ||
* Instead we set a css class | ||
* Set font size css class | ||
*/ | ||
(function(wysihtml5) { | ||
var REG_EXP = /wysiwyg-font-size-[0-9a-z\-]+/g; | ||
|
||
wysihtml5.commands.fontSize = { | ||
exec: function(composer, command, size) { | ||
wysihtml5.commands.formatInline.execWithToggle(composer, command, "span", "wysiwyg-font-size-" + size, REG_EXP); | ||
wysihtml5.commands.formatInline.exec(composer, command, {className: "wysiwyg-font-size-" + size, classRegExp: REG_EXP, toggle: true}); | ||
}, | ||
|
||
state: function(composer, command, size) { | ||
return wysihtml5.commands.formatInline.state(composer, command, "span", "wysiwyg-font-size-" + size, REG_EXP); | ||
return wysihtml5.commands.formatInline.state(composer, command, {className: "wysiwyg-font-size-" + size}); | ||
} | ||
}; | ||
})(wysihtml5); |
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,18 +1,16 @@ | ||
/** | ||
* document.execCommand("foreColor") will create either inline styles (firefox, chrome) or use font tags | ||
* which we don't want | ||
* Instead we set a css class | ||
* Set color css class | ||
*/ | ||
(function(wysihtml5) { | ||
var REG_EXP = /wysiwyg-color-[0-9a-z]+/g; | ||
|
||
wysihtml5.commands.foreColor = { | ||
exec: function(composer, command, color) { | ||
wysihtml5.commands.formatInline.execWithToggle(composer, command, "span", "wysiwyg-color-" + color, REG_EXP); | ||
wysihtml5.commands.formatInline.exec(composer, command, {className: "wysiwyg-color-" + color, classRegExp: REG_EXP, toggle: true}); | ||
}, | ||
|
||
state: function(composer, command, color) { | ||
return wysihtml5.commands.formatInline.state(composer, command, "span", "wysiwyg-color-" + color, REG_EXP); | ||
return wysihtml5.commands.formatInline.state(composer, command, {className: "wysiwyg-color-" + color}); | ||
} | ||
}; | ||
})(wysihtml5); |
Oops, something went wrong.