如何在tiny mce中查找用户是否选择了整个文本或部分文本

发布于 2024-10-04 02:18:07 字数 1434 浏览 1 评论 0原文

我想检查所选文本的节点。如果它是跨度,另一个函数将编辑应用于它的类。如果它与父节点相同,则代码会将所选内容包装在 span 中并设置其样式。我面临的问题是,如何确定用户是否选择了编辑器中的整个文本或仅选择了部分文本。如果用户选择整个文本,我想将样式应用到父节点,而不是向其添加新的跨度和样式。下面是我的代码 -

  var ed=tinyMCE.getInstanceById('textAreaId'); 
  var sel = ed.selection.getContent();

  if(trim(sel)!="") {
  //get the node of the selection
  var thisNode=tinyMCE.activeEditor.selection.getStart().nodeName;   
 ed_Property=propertyName;
 ed_PropertyVal=propertyValue;
 //get the root node inside editor
 var parentNode=tinyMCE.activeEditor.getBody().firstChild.nodeName;
 if(thisNode=="SPAN") {
 nodeclass=$(tinyMCE.activeEditor.selection.getNode()).attr('class');    
//edit span properties
editSpanProperties(nodeclass,propertyName,propertyValue);

}
 else if(thisNode==parentNode) {
  var elmType="span";
  var Spanid1=createSpanId(targetToStyle,elmType);
  Spanid=targetToStyle+"_"+elmType+"_"+Spanid1;
  var strStyle=ed_Property+":"+ed_PropertyVal;
//wrap selection in a span
sel = "<span id='"+Spanid+"' style='"+strStyle+"'>" + sel + "</span>";
//set content    
ed.selection.setContent(sel);
//retain the selection
  ed.selection.select(ed.dom.select('#'+Spanid)[0],false);

}

  else {

     //here I need to check if user has selected whole text and set properties
    setStylesOnEditor(templateSection,propertyName,propertyValue);    
  }//if ends


  }
   else if(trim(sel)=="")   {
   alert('No text selected');
  }

I want to check the node of the selected text. If it is span, another function will edit the classes applied to it. If it is same as the parent node, then the code will wrap the selection in span and set its styles. The problem here I'm facing is, how to determine, if user has selected the whole text inside the editor or only some text. If user selects the whole text, I want to apply the styles to the parent node instead of adding a new span and styles to it. Below is my code -

  var ed=tinyMCE.getInstanceById('textAreaId'); 
  var sel = ed.selection.getContent();

  if(trim(sel)!="") {
  //get the node of the selection
  var thisNode=tinyMCE.activeEditor.selection.getStart().nodeName;   
 ed_Property=propertyName;
 ed_PropertyVal=propertyValue;
 //get the root node inside editor
 var parentNode=tinyMCE.activeEditor.getBody().firstChild.nodeName;
 if(thisNode=="SPAN") {
 nodeclass=$(tinyMCE.activeEditor.selection.getNode()).attr('class');    
//edit span properties
editSpanProperties(nodeclass,propertyName,propertyValue);

}
 else if(thisNode==parentNode) {
  var elmType="span";
  var Spanid1=createSpanId(targetToStyle,elmType);
  Spanid=targetToStyle+"_"+elmType+"_"+Spanid1;
  var strStyle=ed_Property+":"+ed_PropertyVal;
//wrap selection in a span
sel = "<span id='"+Spanid+"' style='"+strStyle+"'>" + sel + "</span>";
//set content    
ed.selection.setContent(sel);
//retain the selection
  ed.selection.select(ed.dom.select('#'+Spanid)[0],false);

}

  else {

     //here I need to check if user has selected whole text and set properties
    setStylesOnEditor(templateSection,propertyName,propertyValue);    
  }//if ends


  }
   else if(trim(sel)=="")   {
   alert('No text selected');
  }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

生寂 2024-10-11 02:18:07

如何确定用户是否已选择
编辑器内的整个文本或
只有一些文字。

您需要将所选文本与编辑器内容进行比较:

var content_editor = $(ed.getBody()).text();

var content_selection = ed.selection.getContent({format : 'text'});

// now compare both either characterwise or as whole string, 
// you might need to strip whitespace characters from the strings!
// and do what you want to do in each case

how to determine, if user has selected
the whole text inside the editor or
only some text.

You need to compare the selected text with the editors content:

var content_editor = $(ed.getBody()).text();

var content_selection = ed.selection.getContent({format : 'text'});

// now compare both either characterwise or as whole string, 
// you might need to strip whitespace characters from the strings!
// and do what you want to do in each case
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文