Skip to content

Commit

Permalink
fix: shift when selection futurepress#1366
Browse files Browse the repository at this point in the history
  • Loading branch information
Anxcye committed Jul 28, 2024
1 parent 39e3daa commit cb534d9
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module.exports = {
"rules": {
"indent": [
"error",
"tab",
"space",
{ "VariableDeclarator": { "var": 2, "let": 2, "const": 3 } }
],
"linebreak-style": [
Expand Down
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"commentTranslate.targetLanguage": "zh-CN"
}
23 changes: 21 additions & 2 deletions src/contents.js
Original file line number Diff line number Diff line change
Expand Up @@ -928,7 +928,24 @@ class Contents {
return;
}
this._onSelectionChange = this.onSelectionChange.bind(this);
this.document.addEventListener("selectionchange", this._onSelectionChange, { passive: true });
this.document.addEventListener("selectionchange", this._onSelectionChange, { passive: true });
// https://github.com/futurepress/epub.js/issues/1366#issuecomment-2026512757
// 添加下面的方法
this.document.addEventListener("selectstart", (event)=>{
const targetNode = document.querySelector(".epub-container");
const sourceLeft = targetNode.scrollLeft
targetNode.style.overflow = "auto";
// 选文字后,开始滚动,说明跨区了进行打断
targetNode.addEventListener("scroll",(event2)=>{
// console.log("scroll",event2.target.scrollLeft)
targetNode.style.overflow = "hidden";
var selection = this.window.getSelection();
//var r = selection.getRangeAt(0);
selection.removeAllRanges();
targetNode.scrollLeft = sourceLeft
},{ capture: false, once:true })
// 添加结束
}, { passive: true });
}

/**
Expand All @@ -953,7 +970,9 @@ class Contents {
}
this.selectionEndTimeout = setTimeout(function() {
var selection = this.window.getSelection();
this.triggerSelectedEvent(selection);
this.triggerSelectedEvent(selection);
const targetNode = document.querySelector(".epub-container");
targetNode.style.overflow = "hidden";
}.bind(this), 250);
}

Expand Down
8 changes: 7 additions & 1 deletion src/utils/replacements.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,13 @@ export function replaceLinks(contents, fn) {
}

var base = qs(contents.ownerDocument, "base");
var location = base ? base.getAttribute("href") : undefined;
var location = base ? base.getAttribute("href") : undefined;
// 我在webview中得到的location变量的值为: "null/OEBPS/Text/part0001.xhtml" 正常情况下应当是:"file:///OEBPS/Text/part0001.xhtml",这是怎么回事?
// 前四个字符串如果为null,则将其替换为file://
// TODO 为什么会出现这种情况?
if (location && location.substring(0, 4) === "null") {
location = "file://" + location.substring(4);
}
var replaceLink = function(link){
var href = link.getAttribute("href");

Expand Down

0 comments on commit cb534d9

Please sign in to comment.