Skip to content
This repository has been archived by the owner on Mar 8, 2019. It is now read-only.

Commit

Permalink
Remove useless 'state' function from commands
Browse files Browse the repository at this point in the history
  • Loading branch information
tiff committed Jul 16, 2012
1 parent 94ef126 commit 2c6dc01
Show file tree
Hide file tree
Showing 19 changed files with 166 additions and 299 deletions.
23 changes: 0 additions & 23 deletions src/commands.js
Expand Up @@ -78,28 +78,5 @@ wysihtml5.Commands = Base.extend(
return false;
}
}
},

/**
* Get the current command's value
*
* @param {String} command The command string which to check (eg. "formatBlock")
* @return {String} The command value
* @example
* var currentBlockElement = commands.value("formatBlock");
*/
value: function(command) {
var obj = wysihtml5.commands[command],
method = obj && obj.value;
if (method) {
return method.call(obj, this.composer, command);
} else {
try {
// try/catch for buggy firefox
return this.doc.queryCommandValue(command);
} catch(e) {
return null;
}
}
}
});
34 changes: 13 additions & 21 deletions src/commands/bold.js
@@ -1,23 +1,15 @@
(function(wysihtml5) {
var undef;

wysihtml5.commands.bold = {
exec: function(composer, command) {
return wysihtml5.commands.formatInline.exec(composer, command, "b");
},
wysihtml5.commands.bold = {
exec: function(composer, command) {
return wysihtml5.commands.formatInline.exec(composer, command, "b");
},

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");
},

value: function() {
return undef;
}
};
})(wysihtml5);
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");
}
};

4 changes: 0 additions & 4 deletions src/commands/createLink.js
Expand Up @@ -96,10 +96,6 @@

state: function(composer, command) {
return wysihtml5.commands.formatInline.state(composer, command, "A");
},

value: function() {
return undef;
}
};
})(wysihtml5);
7 changes: 1 addition & 6 deletions src/commands/foreColor.js
Expand Up @@ -4,8 +4,7 @@
* Instead we set a css class
*/
(function(wysihtml5) {
var undef,
REG_EXP = /wysiwyg-color-[0-9a-z]+/g;
var REG_EXP = /wysiwyg-color-[0-9a-z]+/g;

wysihtml5.commands.foreColor = {
exec: function(composer, command, color) {
Expand All @@ -14,10 +13,6 @@

state: function(composer, command, color) {
return wysihtml5.commands.formatInline.state(composer, command, "span", "wysiwyg-color-" + color, REG_EXP);
},

value: function() {
return undef;
}
};
})(wysihtml5);
7 changes: 1 addition & 6 deletions src/commands/formatBlock.js
@@ -1,6 +1,5 @@
(function(wysihtml5) {
var undef,
dom = wysihtml5.dom,
var dom = wysihtml5.dom,
DEFAULT_NODE_NAME = "DIV",
// Following elements are grouped
// when the caret is within a H1 and the H4 is invoked, the H1 should turn into H4
Expand Down Expand Up @@ -217,10 +216,6 @@
className: className,
classRegExp: classRegExp
});
},

value: function() {
return undef;
}
};
})(wysihtml5);
7 changes: 1 addition & 6 deletions src/commands/formatInline.js
Expand Up @@ -32,8 +32,7 @@
* <span>ab|c</span> de|<b>fgh</b>
*/
(function(wysihtml5) {
var undef,
// Treat <b> as <strong> and vice versa
var // Treat <b> as <strong> and vice versa
ALIAS_MAPPING = {
"strong": "b",
"em": "i",
Expand Down Expand Up @@ -87,10 +86,6 @@
}

return _getApplier(tagName, className, classRegExp).isAppliedToRange(range);
},

value: function() {
return undef;
}
};
})(wysihtml5);
32 changes: 12 additions & 20 deletions src/commands/insertHTML.js
@@ -1,21 +1,13 @@
(function(wysihtml5) {
var undef;

wysihtml5.commands.insertHTML = {
exec: function(composer, command, html) {
if (composer.commands.support(command)) {
composer.doc.execCommand(command, false, html);
} else {
composer.selection.insertHTML(html);
}
},

state: function() {
return false;
},

value: function() {
return undef;
wysihtml5.commands.insertHTML = {
exec: function(composer, command, html) {
if (composer.commands.support(command)) {
composer.doc.execCommand(command, false, html);
} else {
composer.selection.insertHTML(html);
}
};
})(wysihtml5);
},

state: function() {
return false;
}
};
5 changes: 0 additions & 5 deletions src/commands/insertImage.js
Expand Up @@ -97,11 +97,6 @@
}

return imagesInSelection[0];
},

value: function(composer) {
var image = this.state(composer);
return image && image.src;
}
};
})(wysihtml5);
7 changes: 1 addition & 6 deletions src/commands/insertLineBreak.js
@@ -1,6 +1,5 @@
(function(wysihtml5) {
var undef,
LINE_BREAK = "<br>" + (wysihtml5.browser.needsSpaceAfterLineBreak() ? " " : "");
var LINE_BREAK = "<br>" + (wysihtml5.browser.needsSpaceAfterLineBreak() ? " " : "");

wysihtml5.commands.insertLineBreak = {
exec: function(composer, command) {
Expand All @@ -16,10 +15,6 @@

state: function() {
return false;
},

value: function() {
return undef;
}
};
})(wysihtml5);
104 changes: 48 additions & 56 deletions src/commands/insertOrderedList.js
@@ -1,58 +1,50 @@
(function(wysihtml5) {
var undef;

wysihtml5.commands.insertOrderedList = {
exec: function(composer, command) {
var doc = composer.doc,
selectedNode = composer.selection.getSelectedNode(),
list = wysihtml5.dom.getParentElement(selectedNode, { nodeName: "OL" }),
otherList = wysihtml5.dom.getParentElement(selectedNode, { nodeName: "UL" }),
tempClassName = "_wysihtml5-temp-" + new Date().getTime(),
isEmpty,
tempElement;

if (composer.commands.support(command)) {
doc.execCommand(command, false, null);
return;
}

if (list) {
// Unwrap list
// <ol><li>foo</li><li>bar</li></ol>
// becomes:
// foo<br>bar<br>
composer.selection.executeAndRestoreSimple(function() {
wysihtml5.dom.resolveList(list);
});
} else if (otherList) {
// Turn an unordered list into an ordered list
// <ul><li>foo</li><li>bar</li></ul>
// becomes:
// <ol><li>foo</li><li>bar</li></ol>
composer.selection.executeAndRestoreSimple(function() {
wysihtml5.dom.renameElement(otherList, "ol");
});
} else {
// Create list
composer.commands.exec("formatBlock", "div", tempClassName);
tempElement = doc.querySelector("." + tempClassName);
isEmpty = tempElement.innerHTML === "" || tempElement.innerHTML === wysihtml5.INVISIBLE_SPACE;
composer.selection.executeAndRestoreSimple(function() {
list = wysihtml5.dom.convertToList(tempElement, "ol");
});
if (isEmpty) {
composer.selection.selectNode(list.querySelector("li"));
}
}
},
wysihtml5.commands.insertOrderedList = {
exec: function(composer, command) {
var doc = composer.doc,
selectedNode = composer.selection.getSelectedNode(),
list = wysihtml5.dom.getParentElement(selectedNode, { nodeName: "OL" }),
otherList = wysihtml5.dom.getParentElement(selectedNode, { nodeName: "UL" }),
tempClassName = "_wysihtml5-temp-" + new Date().getTime(),
isEmpty,
tempElement;

state: function(composer) {
var selectedNode = composer.selection.getSelectedNode();
return wysihtml5.dom.getParentElement(selectedNode, { nodeName: "OL" });
},

value: function() {
return undef;
if (composer.commands.support(command)) {
doc.execCommand(command, false, null);
return;
}
};
})(wysihtml5);

if (list) {
// Unwrap list
// <ol><li>foo</li><li>bar</li></ol>
// becomes:
// foo<br>bar<br>
composer.selection.executeAndRestoreSimple(function() {
wysihtml5.dom.resolveList(list);
});
} else if (otherList) {
// Turn an unordered list into an ordered list
// <ul><li>foo</li><li>bar</li></ul>
// becomes:
// <ol><li>foo</li><li>bar</li></ol>
composer.selection.executeAndRestoreSimple(function() {
wysihtml5.dom.renameElement(otherList, "ol");
});
} else {
// Create list
composer.commands.exec("formatBlock", "div", tempClassName);
tempElement = doc.querySelector("." + tempClassName);
isEmpty = tempElement.innerHTML === "" || tempElement.innerHTML === wysihtml5.INVISIBLE_SPACE;
composer.selection.executeAndRestoreSimple(function() {
list = wysihtml5.dom.convertToList(tempElement, "ol");
});
if (isEmpty) {
composer.selection.selectNode(list.querySelector("li"));
}
}
},

state: function(composer) {
var selectedNode = composer.selection.getSelectedNode();
return wysihtml5.dom.getParentElement(selectedNode, { nodeName: "OL" });
}
};

0 comments on commit 2c6dc01

Please sign in to comment.