document.getElementById("info").innerHTML += "paper width (px)" + document.getElementById("paper").offsetWidth + "
";
function css(element, property) {
return window.getComputedStyle(element, null).getPropertyValue(property);
}
var paper = document.getElementById('paper');
var paperPage1 = document.getElementById('paper-page1');
var font_family = css(paper, 'font-family');
var font_size = css(paper, 'font-size');
paper.addEventListener('input', getWordsWidth);
paper.addEventListener('keydown', function (e) {
if (e.key == " ") { //enter
e.preventDefault(); //Prevent default browser behavior
var sel;
var range;
sel = window.getSelection();
range = sel.getRangeAt(0);
range.deleteContents();
var textNode = document.createTextNode('\u00A0'); // no linebreaking-space
range.insertNode(textNode);
range.setStartAfter(textNode);
sel.addRange(range);
getWordsWidth();
// paper.innerHTML+= paper.innerHTML;
//var currentPage = document.getElementById(getCaretCurrentPageId());
//var currentRange = getCursorLengthRange(currentPage);
//var HTMLBeforeCaret = rangeToHTMLFragement(currentRange);
}
});
// 如果游標在 contentdiveditable id "paper-page1" 裡面,
// 該函數返回 "paper-page1"
function getCaretCurrentPageId() {
var anchorNode = window.getSelection().anchorNode;
var currentNode = anchorNode;
while (currentNode.id === undefined || !(currentNode.id.match(/^paper-page\d+/))) {
currentNode = currentNode.parentNode;
}
return currentNode.id;
}
// 得到子節點(文字物件)
// ["dolore", [ "m ips", ["um si"], "t am"], "et"]
function getChildNodes(ele) {
var res_array;
res_array = [];
if (ele.nodeType == Node.TEXT_NODE) {
return ele;
}
else {
for (var i = 0; i < ele.childNodes.length; i++) {
res_array.push(getChildNodes((ele.childNodes[i])));
}
return res_array;
}
}
// 平坦化陣列
// [a, [b, c, d], [[e]]] => [a, b, c, d, e]
function flatten(arr) {
if (!Array.isArray(arr)) {
return arr;
}
else {
var result = [];
for (var i = 0; i < arr.length; i++) {
if (Array.isArray(arr[i])) {
result = result.concat(flatten(arr[i]));
}
else {
result.push(arr[i]);
}
}
return result;
}
}
/* 從第幾個字到第幾個字選 range. */
function selecRengeFromCharToChar(i, j, ele) {
var range = document.createRange();
var sel = document.getSelection();
var flattened_ar = flatten(getChildNodes(ele));
range.selectNodeContents(ele);
var ran_start = i;
var ran_end = j;
var ran_start_is_set = false;
for (var i = 0; i < flattened_ar.length; i++) {
var fragement_text_length = flattened_ar[i].textContent.length;
// 如果 ran_start > 碎片長度
if (ran_start > fragement_text_length) {
ran_start -= fragement_text_length;
ran_end -= fragement_text_length;
// 如果 ran_start <= 碎片長度
}
else if (ran_start <= fragement_text_length && ran_start_is_set == false) {
range.setStart(flattened_ar[i], ran_start);
ran_start_is_set = true;
// 如果 ran_end > 碎片長度
if (ran_end > fragement_text_length) {
ran_end -= fragement_text_length;
// 如果 ran_end <= 碎片長度,setEnd 中止迴圈
}
else {
range.setEnd(flattened_ar[i], ran_end);
break;
}
// 如果 ran_end > 碎片長度
}
else if (ran_end > fragement_text_length && ran_start_is_set == true) {
ran_end -= fragement_text_length;
}
// 如果 ran_end <= 碎片長度,setEnd 中止迴圈
else {
range.setEnd(flattened_ar[i], ran_end);
break;
}
}
var sel = window.getSelection();
sel.removeAllRanges();
sel.addRange(range);
return range;
}
/*
設置游標位置於 ele 要素的第 col 個字(不含標籤),例子:
HTML