RCP: X.X, edition annotation V1
Soit le retour de la sélection rend compte des limites de mots : pas du tout
Le résultat de la sélection correspond à une séquence d’occurences de mots projetée sur une propriété : word - qui correspond à la surface sélectionnée modulo recalage.
Corriger l’appel de la Concordance.
note: works only if anchor node preceeds focus node
SH: Could use Node.compareDocumentPosition() to decide how to move in
the DOM (’next|previous’Sibling). See
https://groupes.renater.fr/wiki/txm-info/documentation_web.
JS code to get selection content (FF, IE11+, Safari)
var all = ""
var sel = window.getSelection();
var node = sel.anchorNode.parentNode;
while (node != sel.focusNode.parentNode) {
all += node.textContent
node = node.nextSibling;
}
all += sel.focusNode.parentNode.textContent
alert(all);
JS code to retrive id from selected span
function getID(node) {
if (node.nodeType == 1) {
for (i = 0; i < node.attributes.length; i++) {
if (node.attributes[i].name == "id") {
return node.attributes[i].value;
}
}
}
return ""
}
function myFunction() {
var all = []
var sel = window.getSelection();
var node = sel.anchorNode.parentNode;
while (node != sel.focusNode.parentNode) {
all.push(getID(node))
node = node.nextSibling;
}
all += getID(sel.focusNode.parentNode)
alert(all);
}
Solution
Previous solutions don’t manage word half selected.
The solution is to use the first Range
(https://developer.mozilla.org/en-US/docs/Web/API/Range) of the
Selection (https://developer.mozilla.org/en-US/docs/Web/API/Selection)
to get the id of the first word span and the id of the last word span.
Then resolve the CQP “word” properties of positions between
pos(START_SPAN_WORD_ID) and pos(END_SPAN_WORD_ID)
function findSpans(c, a) {
for (var i = 0 ; i < c.length ; i++) {
if (c.item(i).tagName == "SPAN") {
var id = c.item(i).getAttribute("id")
if (id.indexOf("w_") == 0)
a.push(id)
} else if (c.item(i).nodeType == 1) {
findSpans(c.item(i).children, a)
}
}
}
function getWordIDFromSelection() {
var all = [];
var sel = window.getSelection();
if (sel.rangeCount == 0){
return all;
}
var range = sel.getRangeAt(0);
var frag = range.cloneContents();
findSpans(frag.children, all);
return all
}
(from redmine: issue id 1545, created on 2015/10/01 by Serge Heiden)
- Relations:
- parent #1544