Skip to content

Commit

Permalink
fix: fix window, document owner #132
Browse files Browse the repository at this point in the history
  • Loading branch information
daybrush committed May 30, 2023
1 parent 11ec4aa commit aff5069
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 27 deletions.
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
]
},
"resolutions": {
"@daybrush/utils": "^1.7.1",
"@daybrush/utils": "^1.12.0",
"@types/react": "^16.9.17",
"typescript": "^4.5.0 <4.6.0",
"@storybook/react": "^6.4.22",
Expand All @@ -84,12 +84,12 @@
"@scena/matrix": "^1.1.1",
"css-to-mat": "^1.0.3",
"framework-utils": "^1.1.0",
"gesto": "^1.15.1",
"gesto": "^1.18.1",
"overlap-area": "^1.1.0",
"tslib": "^2.3.0"
},
"overrides": {
"@daybrush/utils": "^1.7.1",
"@daybrush/utils": "^1.12.0",
"@types/react": "^16.9.17",
"typescript": "^4.5.0 <4.6.0",
"@storybook/react": "^6.4.22",
Expand All @@ -105,7 +105,7 @@
"@scena/matrix": "^1.1.1",
"css-to-mat": "^1.0.3",
"framework-utils": "^1.1.0",
"gesto": "^1.15.1",
"gesto": "^1.18.1",
"overlap-area": "^1.1.0",
"tslib": "^2.3.0"
}
Expand Down
4 changes: 2 additions & 2 deletions packages/selecto/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@
"dist/*"
],
"dependencies": {
"@daybrush/utils": "^1.7.1",
"@daybrush/utils": "^1.12.0",
"@egjs/children-differ": "^1.0.1",
"@scena/dragscroll": "^1.4.0",
"@scena/event-emitter": "^1.0.5",
"css-styled": "^1.0.7",
"css-to-mat": "^1.0.3",
"framework-utils": "^1.1.0",
"gesto": "^1.15.1",
"gesto": "^1.18.1",
"keycon": "^1.2.0",
"overlap-area": "^1.1.0"
},
Expand Down
14 changes: 8 additions & 6 deletions packages/selecto/src/SelectoManager.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import {
between,
splitUnit,
isFunction,
getWindow,
getDocument,
} from "@daybrush/utils";
import { diff } from "@egjs/children-differ";
import DragScroll from "@scena/dragscroll";
Expand All @@ -35,7 +37,6 @@ import {
elementFromPoint,
filterDuplicated,
getLineSize,
getDocument,
} from "./utils";
import {
SelectoOptions,
Expand Down Expand Up @@ -443,7 +444,7 @@ class Selecto extends EventEmitter<SelectoEvents> {
this.keycon = null;
}
if (toggleContinueSelect || toggleContinueSelectWithoutDeselect) {
this.keycon = new KeyController(keyContainer || window);
this.keycon = new KeyController(keyContainer || getWindow(this.container));
this.keycon
.keydown(this._onKeyDown)
.keyup(this._onKeyUp)
Expand Down Expand Up @@ -544,7 +545,7 @@ class Selecto extends EventEmitter<SelectoEvents> {
: dragContainer || (this.target.parentNode as any);
this.gesto = new Gesto(this.dragContainer, {
checkWindowBlur: true,
container: window,
container: getWindow(container),
checkInput,
preventDefault,
preventClickEventOnDragStart,
Expand Down Expand Up @@ -911,8 +912,9 @@ class Selecto extends EventEmitter<SelectoEvents> {
return;
}
data.data = {};
data.innerWidth = window.innerWidth;
data.innerHeight = window.innerHeight;
const win = getWindow(this.container);
data.innerWidth = win.innerWidth;
data.innerHeight = win.innerHeight;
this.findSelectableTargets(data);
data.startSelectedTargets = this.selectedTargets;
data.scaleMatrix = createMatrix();
Expand Down Expand Up @@ -1414,7 +1416,7 @@ class Selecto extends EventEmitter<SelectoEvents> {
}
let dragContainer = this.dragContainer;

if (dragContainer === window) {
if (dragContainer === getWindow(this.container)) {
dragContainer = doc.documentElement;
}
const containers =
Expand Down
6 changes: 1 addition & 5 deletions packages/selecto/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Hypertext, Point, Rect } from "./types";
import { IObject, addClass, hasClass, calculateBoundSize, getDist } from "@daybrush/utils";
import { IObject, addClass, hasClass, calculateBoundSize, getDist, getDocument } from "@daybrush/utils";
import { diff } from "@egjs/children-differ";
import { getMinMaxs } from "overlap-area";

Expand Down Expand Up @@ -214,7 +214,3 @@ export function getLineSize(points: number[][]) {

return size;
}

export function getDocument(el: Node) {
return el.ownerDocument || document;
}
77 changes: 77 additions & 0 deletions packages/selecto/test/manual/iframe.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@

<iframe width="1000" height="1000" id="iframe"></iframe>
<script src="../../dist/selecto.js"></script>
<script>
const iframeWindow = iframe.contentWindow;
const iframeDocument = iframe.contentDocument;

iframeDocument.body.innerHTML = `
<style>
.target,
.target2 {
position: absolute;
background: #faa;
box-sizing: border-box;
}
.target.selected {
border: 4px solid #f55;
}
</style>
<div class="target" style="top: 50px; left: 50px; width: 50px; height: 50px">T1</div>
<div class="target" style="top: 50px; left: 150px; width: 150px; height: 50px">T2</div>
<div class="target" style="top: 150px; left: 50px; width: 100px; height: 50px">T3</div>
<div class="target" style="top: 300px; left: 250px; width: 50px; height: 150px">T4</div>
<div class="target" style="top: 200px; left: 400px; width: 100px; height: 0px"></div>
<div class="target2" style="top: 330px; left: 80px; width: 120px; height: 120px" contenteditable="true">T7</div>
`;

const selecto = new Selecto({
container: iframeDocument.body,
dragContainer: iframeWindow,
hitRate: 0,
ratio: 0,
selectableTargets: [".target"],
selectFromInside: false,
selectByClick: true,
toggleContinueSelect: "shift",
// continueSelectWithoutDeselect: true,
}).on("dragStart", e => {
console.log("ds", e.inputEvent.target);
}).on("drag", e => {
selecto.findSelectableTargets();
}).on("selectStart", e => {
console.log("start", e);
e.added.forEach(el => {
el.classList.add("selected");
});
e.removed.forEach(el => {
el.classList.remove("selected");
});
}).on("selectStart", () => {
setTimeout(() => {
document.body.insertAdjacentHTML("beforeend", `<div class="target" style="top: 300px; left: 50px; width: 50px; height: 50px">T6</div>`);
});
}).on("select", e => {
console.log(e.removed, e.added);
}).on("selectEnd", e => {
if (e.isDouble) {
console.log("dblclick");
}
console.log("end", e);
e.afterAdded.forEach(el => {
el.classList.add("selected");
});
e.afterRemoved.forEach(el => {
el.classList.remove("selected");
});
}).on("keydown", () => {
document.querySelector(".button").classList.add("selected");
}).on("keyup", () => {
document.querySelector(".button").classList.remove("selected");
});
</script>
36 changes: 26 additions & 10 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1916,10 +1916,10 @@
dependencies:
prototype-minify "^1.0.0"

"@daybrush/utils@^0.10.0", "@daybrush/utils@^0.10.1", "@daybrush/utils@^0.11.0", "@daybrush/utils@^1.0.0", "@daybrush/utils@^1.1.1", "@daybrush/utils@^1.11.0", "@daybrush/utils@^1.3.1", "@daybrush/utils@^1.4.0", "@daybrush/utils@^1.6.0", "@daybrush/utils@^1.7.1":
version "1.7.1"
resolved "https://registry.npmjs.org/@daybrush/utils/-/utils-1.7.1.tgz"
integrity sha512-ruVDIfXeVAF4s0RxJoNx5hTjxlIRMnKoJ7N5e2m9eDyluIXB12EvhMPQdoq4a/ohJ+cPgj2MiWS5Lvmpsrx8Gg==
"@daybrush/utils@^0.10.0", "@daybrush/utils@^0.10.1", "@daybrush/utils@^0.11.0", "@daybrush/utils@^1.0.0", "@daybrush/utils@^1.1.1", "@daybrush/utils@^1.11.0", "@daybrush/utils@^1.12.0", "@daybrush/utils@^1.3.1", "@daybrush/utils@^1.4.0", "@daybrush/utils@^1.6.0", "@daybrush/utils@^1.7.1":
version "1.12.0"
resolved "https://registry.npmjs.org/@daybrush/utils/-/utils-1.12.0.tgz#b0616a797e46c6fba070bd976119684e0656dcb7"
integrity sha512-5ir4E+bh8M3BNwkGp/lvKa0xpQe37W4ayUtNlmIt16l0xbRlGhs/62xE0d0Fyebr/+/6gV07R/1MeEcYZ2dtiA==

"@discoveryjs/json-ext@0.5.6":
version "0.5.6"
Expand Down Expand Up @@ -11409,12 +11409,12 @@ gensync@^1.0.0-beta.1, gensync@^1.0.0-beta.2:
resolved "https://registry.npmjs.org/gensync/-/gensync-1.0.0-beta.2.tgz"
integrity sha512-3hN7NaskYvMDLQY55gnW3NQ+mesEAepTqlg+VEbj7zzqEMBVNhzcGYYeqFo/TlYz6eQiFcp1HcsCZO+nGgS8zg==

gesto@^1.15.1, gesto@^1.2.1, gesto@^1.3.0, gesto@^1.9.0:
version "1.15.1"
resolved "https://registry.npmjs.org/gesto/-/gesto-1.15.1.tgz#16eb927f5319995abca37bafafe90d423be0becf"
integrity sha512-mqdBeVeYpymupHojEEyH33u0KvL/hbG6gKbqoQKoR7aeWrN82CIXYZkAo4Ie81PUhxS5HNDdmJVcSzqOpPm0JQ==
gesto@^1.18.1, gesto@^1.2.1, gesto@^1.3.0, gesto@^1.9.0:
version "1.18.1"
resolved "https://registry.npmjs.org/gesto/-/gesto-1.18.1.tgz#8d026f4b7e4ba5118a0e1fdef411d33fc33a9cc9"
integrity sha512-1R7dozywz1NPqLvBlpLZdcIuq6xC+aW70364Jm5RicipO2pl3ruwCyzNxdgRHFfNJLIF4R3GaQpN9LvSbuNUvA==
dependencies:
"@daybrush/utils" "^1.7.1"
"@daybrush/utils" "^1.12.0"
"@scena/event-emitter" "^1.0.2"

get-caller-file@^2.0.1, get-caller-file@^2.0.5:
Expand Down Expand Up @@ -12988,6 +12988,13 @@ is-color-stop@^1.0.0:
rgb-regex "^1.0.1"
rgba-regex "^1.0.0"

is-core-module@^2.11.0:
version "2.12.1"
resolved "https://registry.npmjs.org/is-core-module/-/is-core-module-2.12.1.tgz#0c0b6885b6f80011c71541ce15c8d66cf5a4f9fd"
integrity sha512-Q4ZuBAe2FUsKtyQJoQHlvP8OvBERxO3jEmy1I7hcRXcJBGGHFh/aJBswbXuS9sgrDH2QUO8ilkwNPHvHMd8clg==
dependencies:
has "^1.0.3"

is-core-module@^2.2.0, is-core-module@^2.5.0, is-core-module@^2.9.0:
version "2.9.0"
resolved "https://registry.npmjs.org/is-core-module/-/is-core-module-2.9.0.tgz"
Expand Down Expand Up @@ -20106,7 +20113,7 @@ resolve@1.20.0:
is-core-module "^2.2.0"
path-parse "^1.0.6"

resolve@^1.1.7, resolve@^1.10.0, resolve@^1.11.0, resolve@^1.11.1, resolve@^1.12.0, resolve@^1.14.2, resolve@^1.15.1, resolve@^1.19.0, resolve@^1.20.0, resolve@^1.3.2, resolve@^1.8.1:
resolve@^1.1.7, resolve@^1.10.0, resolve@^1.11.0, resolve@^1.11.1, resolve@^1.12.0, resolve@^1.14.2, resolve@^1.19.0, resolve@^1.20.0, resolve@^1.3.2, resolve@^1.8.1:
version "1.22.1"
resolved "https://registry.npmjs.org/resolve/-/resolve-1.22.1.tgz"
integrity sha512-nBpuuYuY5jFsli/JIs1oldw6fOQCBioohqWZg/2hiaOybXOft4lonv85uDOKXdf8rhyK159cxU5cDcK/NKk8zw==
Expand All @@ -20115,6 +20122,15 @@ resolve@^1.1.7, resolve@^1.10.0, resolve@^1.11.0, resolve@^1.11.1, resolve@^1.12
path-parse "^1.0.7"
supports-preserve-symlinks-flag "^1.0.0"

resolve@^1.18.1:
version "1.22.2"
resolved "https://registry.npmjs.org/resolve/-/resolve-1.22.2.tgz#0ed0943d4e301867955766c9f3e1ae6d01c6845f"
integrity sha512-Sb+mjNHOULsBv818T40qSPeRiuWLyaGMa5ewydRLFimneixmVy2zdivRl+AF6jaYPC8ERxGDmFSiqui6SfPd+g==
dependencies:
is-core-module "^2.11.0"
path-parse "^1.0.7"
supports-preserve-symlinks-flag "^1.0.0"

restore-cursor@^2.0.0:
version "2.0.0"
resolved "https://registry.npmjs.org/restore-cursor/-/restore-cursor-2.0.0.tgz"
Expand Down

0 comments on commit aff5069

Please sign in to comment.