*/
function setCaret(ele: ElementContentEditable, col : number){
var range = document.createRange();
var sel = document.getSelection();
var flattened_ar = flatten(getChildNodes(
ele)); // flattened array
range.selectNodeContents((ele));
var ran_start = col
for (var i=0; i< flattened_ar.length;i++){
var fragement_text_length = flattened_ar[i].textContent.length;
if(ran_start > fragement_text_length){
ran_start -= fragement_text_length;
}else{
range.setEnd(flattened_ar[i], ran_start);
range.collapse();
break;
}
}
var sel = window.getSelection();
sel.removeAllRanges();
sel.addRange(range);
}
// 將座標轉成座標所在位置的 range
function getCursorLengthRange(ele: HTMLElement){
let win = ele.ownerDocument.defaultView;
let range = win.getSelection().getRangeAt(0);
let cloneRange = range.cloneRange();
cloneRange.selectNodeContents(ele);
cloneRange.setEnd(range.endContainer, range.endOffset);
return cloneRange;
}
// 將座標 range 轉成字串的長度
function rangeToStringLen(range : Range){
return range.toString().length;
}
// 將座標位置 range 轉成 html 斷片
function rangeToHTMLFragement(range : Range){
var nodes =range.cloneContents().childNodes;
var result = ""
for(var i=0;i nodes[i].parentNode);
let div=document.createElement("div");
div.appendChild(parentNode);
if (typeof (( nodes[i]).outerHTML) === "undefined"){
result += nodes[i].textContent;}
else{
result += ( nodes[i]).outerHTML;}
console.log("Parent Node"+div.outerHTML);
}
return result;
}
// 得到一個 word 或是 character 的寬度
function getTextWidth(text, fontfamily, fontsize) {
console.log(text)
var decodedText = decodeEntities(text)
let canvas = document.createElement("canvas");
var context = canvas.getContext("2d");
context.font = fontsize + " " + fontfamily;
let width = context.measureText(decodedText).width;
return width;
}
// 得到字寬
function getWordsWidth(){
document.getElementById('line-info').innerHTML = "";
var array = paper.childNodes;
var res = [];
for (var i=0;ix.innerHTML);
var innerHTMLTotal = innerArray.reduce((x,y)=>(x+y));
var innerHTMLRemoveTags = innerHTMLTotal.replace(/[<][\/]?[^<]+[\/]?[>]/gi,"") // 移除標籤
var innerHTMLSplitted = innerHTMLRemoveTags.split(/( |\s| |[⺀-\u2efe\u3000-〾㇀-\u31ee㌀-㏾㐀-\u4dbe一-\u9ffe豈-\ufafe︰-﹎]|[\ud840-\ud868\ud86a-\ud86c][\udc00-\udfff]|\ud869[\udc00-\udede\udf00-\udfff]|\ud86d[\udc00-\udf3e\udf40-\udfff]|\ud86e[\udc00-\udc1e]|\ud87e[\udc00-\ude1e])/).filter(item => item != "")
console.log(innerHTMLSplitted);
//var innerHTMLSplittedLength = innerHTMLSplitted.map((x)=>x.length)
//var innerHTMLSelectedRange = [];
/*var sum = 0
for (var i=0; i rangeToHTMLFragement(selecRengeFromCharToChar(x[0],x[1],paper)))
console.log("Range 結果"+innerHTMLSplittedRange);*/
let textWidthList = innerHTMLSplitted.map(x => getTextWidth(x,font_family, font_size));
var lineInfoLength = [];
for (var i=0; i getrealcol(5, "example")
> 5
> getrealcol(5, "example")
> 14
*/
function getrealcol(n, str) {
var arr = str.split(RegExp("(<[^<>]+>)")).map((x) => ({ cont: x, len: x.length }));
var j = 0;
var k = 0;
for (var i = 0; i < arr.length; i++) {
if (arr[i].cont.match(/^[<]/)) { continue; }
else {
if (n > arr[i].len) { n = n - arr[i].len; }
else { j = i; k = n; break; }
};
}
var res_col = 0;
for (var i = 0; i < j; i++) {
res_col += arr[i].len;
}
res_col += k;
return res_col;
}
// 解碼 &1234; 這種東西用
function decodeEntities(encodedString) {
var textArea = document.createElement('textarea');
textArea.innerHTML = encodedString;
return textArea.value;
}