mirror of
https://github.com/ether/etherpad-lite.git
synced 2025-04-21 16:06:16 -04:00
Merge pull request #2013 from ether/getAttributeOnSelection
getAttributeOnSelection function
This commit is contained in:
commit
505dc70df5
3 changed files with 85 additions and 0 deletions
|
@ -2372,6 +2372,73 @@ function Ace2Inner(){
|
|||
}
|
||||
editorInfo.ace_setAttributeOnSelection = setAttributeOnSelection;
|
||||
|
||||
function getAttributeOnSelection(attributeName){
|
||||
if (!(rep.selStart && rep.selEnd)) return;
|
||||
var selectionAllHasIt = true;
|
||||
var withIt = Changeset.makeAttribsString('+', [
|
||||
[attributeName, 'true']
|
||||
], rep.apool);
|
||||
var withItRegex = new RegExp(withIt.replace(/\*/g, '\\*') + "(\\*|$)");
|
||||
|
||||
function hasIt(attribs)
|
||||
{
|
||||
return withItRegex.test(attribs);
|
||||
}
|
||||
|
||||
var selStartLine = rep.selStart[0];
|
||||
var selEndLine = rep.selEnd[0];
|
||||
for (var n = selStartLine; n <= selEndLine; n++)
|
||||
{
|
||||
var opIter = Changeset.opIterator(rep.alines[n]);
|
||||
var indexIntoLine = 0;
|
||||
var selectionStartInLine = 0;
|
||||
var selectionEndInLine = rep.lines.atIndex(n).text.length; // exclude newline
|
||||
if(rep.lines.atIndex(n).text.length == 0){
|
||||
return false; // If the line length is 0 we basically treat it as having no formatting
|
||||
}
|
||||
if(rep.selStart[1] == rep.selEnd[1] && rep.selStart[1] == rep.lines.atIndex(n).text.length){
|
||||
return false; // If we're at the end of a line we treat it as having no formatting
|
||||
}
|
||||
if(rep.selStart[1] == 0 && rep.selEnd[1] == 0){
|
||||
return false; // If we're at the start of a line attributes get confused..
|
||||
}
|
||||
if (n == selStartLine)
|
||||
{
|
||||
selectionStartInLine = rep.selStart[1];
|
||||
}
|
||||
if (n == selEndLine)
|
||||
{
|
||||
selectionEndInLine = rep.selEnd[1];
|
||||
}
|
||||
while (opIter.hasNext())
|
||||
{
|
||||
var op = opIter.next();
|
||||
var opStartInLine = indexIntoLine;
|
||||
var opEndInLine = opStartInLine + op.chars;
|
||||
if (!hasIt(op.attribs))
|
||||
{
|
||||
// does op overlap selection?
|
||||
if (!(opEndInLine <= selectionStartInLine || opStartInLine >= selectionEndInLine))
|
||||
{
|
||||
selectionAllHasIt = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
indexIntoLine = opEndInLine;
|
||||
}
|
||||
if (!selectionAllHasIt)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(selectionAllHasIt){
|
||||
return true;
|
||||
}else{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
editorInfo.ace_getAttributeOnSelection = getAttributeOnSelection;
|
||||
|
||||
function toggleAttributeOnSelection(attributeName)
|
||||
{
|
||||
if (!(rep.selStart && rep.selEnd)) return;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue