- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 tinymce 插件,并且我正在尝试像在 MS Word (shift + f3) 中一样在热键上执行更改字母大小写功能。
我已经设法通过选择来做到这一点,但即使没有选择单词,MS Word 也能正常工作。
它根据光标的当前位置在该位置分离出一个词并将功能应用于该词。我需要同样的 tinymce。
到目前为止我有这个:
editor.addShortcut("ctrl+e", "ll", function () {
var sel = editor.dom.decode(editor.selection.getContent());
if (/^[a-zа-я]+$/g.test(sel)) {
sel = sel.substr(0, 1).toUpperCase() + sel.substr(1);
}
else if (/^[А-ЯA-Z]+$/g.test(sel)) {
sel = sel.toLowerCase();
}
else {
sel = sel.toUpperCase();
}
editor.selection.setContent(sel);
console.log(editor.selection);
editor.save();
editor.isNotDirty = true;
});
最佳答案
以下代码可能会给您想要的结果。它应该在这些情况下工作:
tinymce.init({
selector: "textarea",
plugins: [
"advlist autolink lists link image charmap print preview anchor",
"searchreplace visualblocks code fullscreen",
"insertdatetime media table contextmenu paste"
],
toolbar: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image",
setup: function (editor) {
editor.addShortcut("ctrl+e", "ll", function () {
var isWordChar = function (chr) {
return /\w/.test(chr);
};
var isTextNode = function (node) {
return node.nodeType == 3;
};
var getAllTextNodes = function (result, node) {
if (isTextNode(node)) {
result.push(node);
}
else if (node.childNodes) {
if (node.tagName == 'P') {
result.push(node);
}
var children = node.childNodes;
for (var i = 0; i < children.length; i++) {
getAllTextNodes(result, children[i]);
}
}
return result;
}
// Get current selection parameters
var range = editor.selection.getRng();
var isCollapsed = editor.selection.isCollapsed();
var selStartChildNode = range.startContainer;
var selEndChildNode = range.endContainer;
var selStartOffset = range.startOffset;
var selEndOffset = range.endOffset;
// Retrieve all the text nodes in the editor
var textNodes = [];
getAllTextNodes(textNodes, editor.dom.getRoot());
var selStartNodeIndex = textNodes.indexOf(selStartChildNode);
var selEndNodeIndex = textNodes.indexOf(selEndChildNode);
var wordStartNodeIndex, wordEndNodeIndex;
var wordStartOffset, wordEndOffset;
var wordTextContent = '';
var found = false;
var node, chr, lastCharIndex;
var i, j;
// Find the start of the first selected word
for (i = selStartNodeIndex; i >= 0 && !found; i--)
{
node = textNodes[i];
if (isTextNode(node)) {
wordStartNodeIndex = i;
lastCharIndex = node.textContent.length - 1;
wordStartOffset = Math.max(0, Math.min(lastCharIndex, node == selStartChildNode ? selStartOffset - 1 : lastCharIndex));
for (; wordStartOffset >= 0; wordStartOffset--) {
chr = node.textContent[wordStartOffset];
if (isWordChar(chr)) {
wordTextContent = chr + wordTextContent;
} else {
found = true;
break;
}
}
} else {
found = true;
break;
}
}
wordStartOffset = Math.max(0, wordStartOffset);
var endNodeFound = false;
var pastEndNode = false;
var isAfterSelection = false;
found = false;
// Find the end of the last selected word
for (i = selStartNodeIndex; i < textNodes.length && !found; i++) {
node = textNodes[i];
pastEndNode = endNodeFound;
if (isTextNode(node)) {
wordEndNodeIndex = i;
wordEndOffset = Math.min(node == selStartChildNode ? selStartOffset : 0, node.textContent.length - 1);
endNodeFound = endNodeFound || node == selEndChildNode;
for (; wordEndOffset < node.textContent.length; wordEndOffset++) {
chr = node.textContent[wordEndOffset];
isAfterSelection = pastEndNode || (endNodeFound && wordEndOffset >= selEndOffset - (isCollapsed ? 0 : 1));
if (isWordChar(chr) || !isAfterSelection) {
wordTextContent = wordTextContent + chr;
} else {
found = true;
break;
}
}
} else if (pastEndNode) {
found = true;
break;
}
}
// Determine the case style to be applied
var caseMode = '';
if (/^([a-z0-9]|\W)+$/g.test(wordTextContent)) {
caseMode = 'CapitalizeWords';
}
else if (/^([A-Z0-9]|\W)+$/g.test(wordTextContent)) {
caseMode = 'LowerCase';
}
else {
caseMode = 'UpperCase';
}
var startCharIndex, endCharIndex, currentIsWordChar;
var prevIsWordChar = false;
var content = '';
// Apply the new case style to the selected nodes
for (i = wordStartNodeIndex; i <= wordEndNodeIndex; i++) {
node = textNodes[i];
if (isTextNode(node)) {
startCharIndex = (i == wordStartNodeIndex ? wordStartOffset : 0);
endCharIndex = (i == wordEndNodeIndex ? wordEndOffset : node.textContent.length);
content = node.textContent.substring(0, startCharIndex);
switch (caseMode) {
case 'CapitalizeWords':
for (j = startCharIndex; j < endCharIndex; j++) {
chr = node.textContent[j];
currentIsWordChar = /\w/.test(chr);
content += (currentIsWordChar && !prevIsWordChar ? chr.toUpperCase() : chr);
prevIsWordChar = currentIsWordChar;
}
break;
case 'LowerCase':
content += node.textContent.substring(startCharIndex, endCharIndex).toLowerCase();
break;
case 'UpperCase':
content += node.textContent.substring(startCharIndex, endCharIndex).toUpperCase();
break;
}
content += node.textContent.substring(endCharIndex);
node.textContent = content;
} else {
prevIsWordChar = false;
}
}
// Restore the selection range
range.setStart(selStartChildNode, selStartOffset);
range.setEnd(selEndChildNode, selEndOffset);
editor.selection.setRng(range);
editor.save();
editor.isNotDirty = true;
});
}
});
你可以在this jsfiddle中试试.为了简化测试,正则表达式只考虑标准的美国字符;您可能希望放回出现在您自己的代码中的特殊字符。
关于javascript - 使用热键更改文本大小写(如 MS-Word 中的 Shift+F3),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39404336/
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 要求提供代码的问题必须表现出对所解决问题的最低限度理解。包括尝试过的解决方案、为什么它们不起作用,以及预
出于某种原因,右栏中的精选文章忽略了“#elementtext”和“#elementtext:hover”属性。仅显示“p.element”和“img.element”。 有什么想法吗? 谢谢 - 塔
我有两个值,每个值都来自不同的枚举。我想检查这两者的允许组合,如果没有找到则执行默认操作。我能以某种方式对这两个值进行切换/大小写吗?我想避免使用多个 if/else 语句或遵循位掩码模式的枚举,只是
我需要 where 但 not 大小写。例如,我想找到没有名字“莎士比亚”的戏剧: _.where(listOfPlays, {author: !"Shakespeare", year: 1611})
我想实现一个 parking 场应用所以有一个带5个或更多 parking 位的车库当司机 parking 时,车库中的下一个空闲位置应该分配给他。 所以我有一个带 5 个或更多插槽的 table 上
我想使用 Erlang 来确定传递给函数的变量是否可以被数字整除。我考虑过使用 case 来执行此操作,但是我找不到解决方案。 case 是适合这项工作的工具吗? 示例:将数字传递给函数 f()。如果
我在 phpmyadmin 中创建了一个表,其列名如 first_name、last_name。当我使用命令显示表中的列名时,它会将它们显示为 first_name。 我想显示我的列名称,如 Firs
使用 Swift 4,如何使用这些规则格式化字符串: 如果单词超过 3 个字母,则首字母大写,否则大写 包含像 St-Michel 这样的连字符的 Pascal 大小写单词 我这里有初稿,但我一直在思
这个问题在这里已经有了答案: Why can't the switch statement be applied to strings? (23 个回答) 关闭 8 年前。 大家好 所以我正在尝试对
在 MVC 操作中,我如何访问使用多个同名值提交的“表单数据”中的值? 我做了什么:int、decimal、string 类型的值工作完美。 问题:每个变体都有一个复选框,所以当我尝试获取它时,它只显
while(1) { char buff[1000]; printf("Enter the word: "); fgets(buff, 1000
我有一个 Dllmain,它在线程附加到此 DLL 时分配线程本地存储。代码如下: BOOL APIENTRY DllMain(HMODULE hModule,
我有一个变量名,比如“WARD_VS_VITAL_SIGNS”,我想将它转换为 Pascal 大小写格式:“WardVsVitalSigns” WARD_VS_VITAL_SIGNS -> WardV
我是 Swift 编码的新手,正在尝试弄清楚如何在触摸节点时制作具有开/关功能的循环音频。我认为实现它的最佳方式是通过 SKAudioNode,但我不确定我在以下代码中做错了什么。当在节点上按下时 -
这是我第一次使用这种枚举,具有关联值类型的枚举,我需要根据对象的类型制作一个 switch 语句,我无法做到,这是枚举: enum TypeEnum { case foo(FooClass)
我想从字符串中删除所有下划线,下划线后面的字符为大写。因此,例如:_my_string_ 变为:MyString 同样:my_string 变为 MyString 有没有更简单的方法呢?我目前有以下内
如何在 Java 中将蛇形大小写转换为 Camel 形大小写? 输入:“input_in_snake_case” 输出:“InputInSnakeCase” 最佳答案 Guava通过其CaseForm
我们有一个表auth_group_access,那么如何使用呢? 在使用M方法时,对于带下划线的表名,可以采用如下方法。 M('AuthGroupAccess'); 对应sql语句SQL: S
我正在制作一个 pygame 游戏,每当我运行我的代码时,我都会收到错误 expected ':'。我知道在 match/case block 中使用 [ 和 ] 用于其他用途,但我该如何解决这个问题
有人能告诉我是否可以使用正则表达式将 url 转换为小写? 这是在 html img 标签内,所以我们可以通过标签找到网址。 这是我所拥有的一个例子 我需要在最后小写图像名称。 该文档包含更多 H
我是一名优秀的程序员,十分优秀!