@pip-it-up/core 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +59 -0
- package/dist/index.d.mts +77 -0
- package/dist/index.d.ts +77 -0
- package/dist/index.js +491 -0
- package/dist/index.mjs +458 -0
- package/package.json +46 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Saurabh Shakya and contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
# @pip-it-up/core
|
|
2
|
+
|
|
3
|
+
The vanilla JavaScript engine for the Document Picture-in-Picture API.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @pip-it-up/core
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```javascript
|
|
14
|
+
import { createPip } from '@pip-it-up/core';
|
|
15
|
+
|
|
16
|
+
const contentEl = document.getElementById('my-content');
|
|
17
|
+
const originEl = document.getElementById('my-placeholder');
|
|
18
|
+
|
|
19
|
+
const pip = createPip({
|
|
20
|
+
contentEl,
|
|
21
|
+
originEl,
|
|
22
|
+
mode: 'move', // 'move', 'clone', or 'portal'
|
|
23
|
+
copyStyles: 'sync', // 'sync', 'once', or false
|
|
24
|
+
fallback: 'new-tab' // 'new-tab', 'modal', or 'none'
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
pip.open().then(() => {
|
|
28
|
+
console.log('PiP opened!');
|
|
29
|
+
});
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## API
|
|
33
|
+
|
|
34
|
+
### `createPip(options: PipOptions): PipInstance`
|
|
35
|
+
|
|
36
|
+
Creates a new PiP instance.
|
|
37
|
+
|
|
38
|
+
#### `PipOptions`
|
|
39
|
+
- `contentEl`: The DOM element to move/clone into the PiP window.
|
|
40
|
+
- `originEl`: The placeholder element in the main window to return the content to when closed (required for `mode: 'move'`).
|
|
41
|
+
- `mode`: `'move'` (default), `'clone'`, or `'portal'`.
|
|
42
|
+
- `copyStyles`: `'sync'` (default), `'once'`, or `false`.
|
|
43
|
+
- `fallback`: `'new-tab'` (default), `'modal'`, or `'none'`.
|
|
44
|
+
- `width` / `height`: Initial dimensions.
|
|
45
|
+
- `lockAspectRatio`: Keep the window's aspect ratio fixed.
|
|
46
|
+
- `fixedSize`: Prevent manual resizing.
|
|
47
|
+
- `onPipWindowReady`: Callback fired when the window is fully prepared.
|
|
48
|
+
|
|
49
|
+
#### `PipInstance`
|
|
50
|
+
- `open()`: Requests and opens the PiP window.
|
|
51
|
+
- `close()`: Closes the PiP window.
|
|
52
|
+
- `toggle()`: Toggles the window state.
|
|
53
|
+
- `isOpen()`: Returns boolean.
|
|
54
|
+
- `getPipWindow()`: Returns the Window object or null.
|
|
55
|
+
- `getState()`: Returns the current state.
|
|
56
|
+
- `destroy()`: Cleans up listeners and DOM.
|
|
57
|
+
|
|
58
|
+
### `getPip(id: string): PipInstance | undefined`
|
|
59
|
+
Retrieves a created PiP instance by ID.
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
declare global {
|
|
2
|
+
interface Window {
|
|
3
|
+
documentPictureInPicture?: {
|
|
4
|
+
requestWindow(options?: any): Promise<Window>;
|
|
5
|
+
window: Window | null;
|
|
6
|
+
onenter: ((this: Window, ev: Event) => any) | null;
|
|
7
|
+
};
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
type FallbackMode = "new-tab" | "modal" | "none" | ((ctx: {
|
|
11
|
+
contentEl?: HTMLElement;
|
|
12
|
+
originEl?: HTMLElement;
|
|
13
|
+
options: PipOptions;
|
|
14
|
+
}) => void);
|
|
15
|
+
type DomMode = "move" | "clone" | "portal";
|
|
16
|
+
type CopyStylesMode = "once" | "sync";
|
|
17
|
+
interface PipOptions {
|
|
18
|
+
id?: string;
|
|
19
|
+
width?: number;
|
|
20
|
+
height?: number;
|
|
21
|
+
preferInitialWindowPlacement?: boolean;
|
|
22
|
+
disallowReturnToOpener?: boolean;
|
|
23
|
+
lockAspectRatio?: boolean;
|
|
24
|
+
fixedSize?: boolean;
|
|
25
|
+
copyStyles?: CopyStylesMode;
|
|
26
|
+
mode?: DomMode;
|
|
27
|
+
fallback?: FallbackMode;
|
|
28
|
+
fallbackUrl?: string;
|
|
29
|
+
forwardKeyboardEvents?: boolean;
|
|
30
|
+
restoreScroll?: boolean;
|
|
31
|
+
restoreFocus?: boolean;
|
|
32
|
+
onBeforeOpen?: () => boolean | Promise<boolean>;
|
|
33
|
+
onOpen?: (pipWindow: Window) => void;
|
|
34
|
+
onPipWindowReady?: (pipWindow: Window) => void;
|
|
35
|
+
onClose?: () => void;
|
|
36
|
+
onError?: (err: Error) => void;
|
|
37
|
+
contentEl?: HTMLElement;
|
|
38
|
+
originEl?: HTMLElement;
|
|
39
|
+
}
|
|
40
|
+
interface PipState {
|
|
41
|
+
isOpen: boolean;
|
|
42
|
+
isSupported: boolean;
|
|
43
|
+
pipWindow: Window | null;
|
|
44
|
+
}
|
|
45
|
+
interface PipInstance {
|
|
46
|
+
id: string;
|
|
47
|
+
open: (elements?: {
|
|
48
|
+
contentEl?: HTMLElement;
|
|
49
|
+
originEl?: HTMLElement;
|
|
50
|
+
}) => Promise<void>;
|
|
51
|
+
close: () => void;
|
|
52
|
+
toggle: (elements?: {
|
|
53
|
+
contentEl?: HTMLElement;
|
|
54
|
+
originEl?: HTMLElement;
|
|
55
|
+
}) => Promise<void>;
|
|
56
|
+
isOpen: () => boolean;
|
|
57
|
+
getPipWindow: () => Window | null;
|
|
58
|
+
subscribe: (fn: () => void) => () => void;
|
|
59
|
+
getState: () => PipState;
|
|
60
|
+
updateElements: (elements: {
|
|
61
|
+
contentEl?: HTMLElement;
|
|
62
|
+
originEl?: HTMLElement;
|
|
63
|
+
}) => void;
|
|
64
|
+
destroy: () => void;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
declare const createPip: (options?: PipOptions) => PipInstance;
|
|
68
|
+
|
|
69
|
+
declare const isSupported: () => boolean;
|
|
70
|
+
|
|
71
|
+
declare const registerPip: (id: string, instance: PipInstance) => void;
|
|
72
|
+
declare const unregisterPip: (id: string) => void;
|
|
73
|
+
declare const getPip: (id: string) => PipInstance | null;
|
|
74
|
+
declare const subscribeRegistry: (id: string, fn: () => void) => (() => void);
|
|
75
|
+
declare const clearRegistry: () => void;
|
|
76
|
+
|
|
77
|
+
export { type CopyStylesMode, type DomMode, type FallbackMode, type PipInstance, type PipOptions, type PipState, clearRegistry, createPip, getPip, isSupported, registerPip, subscribeRegistry, unregisterPip };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
declare global {
|
|
2
|
+
interface Window {
|
|
3
|
+
documentPictureInPicture?: {
|
|
4
|
+
requestWindow(options?: any): Promise<Window>;
|
|
5
|
+
window: Window | null;
|
|
6
|
+
onenter: ((this: Window, ev: Event) => any) | null;
|
|
7
|
+
};
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
type FallbackMode = "new-tab" | "modal" | "none" | ((ctx: {
|
|
11
|
+
contentEl?: HTMLElement;
|
|
12
|
+
originEl?: HTMLElement;
|
|
13
|
+
options: PipOptions;
|
|
14
|
+
}) => void);
|
|
15
|
+
type DomMode = "move" | "clone" | "portal";
|
|
16
|
+
type CopyStylesMode = "once" | "sync";
|
|
17
|
+
interface PipOptions {
|
|
18
|
+
id?: string;
|
|
19
|
+
width?: number;
|
|
20
|
+
height?: number;
|
|
21
|
+
preferInitialWindowPlacement?: boolean;
|
|
22
|
+
disallowReturnToOpener?: boolean;
|
|
23
|
+
lockAspectRatio?: boolean;
|
|
24
|
+
fixedSize?: boolean;
|
|
25
|
+
copyStyles?: CopyStylesMode;
|
|
26
|
+
mode?: DomMode;
|
|
27
|
+
fallback?: FallbackMode;
|
|
28
|
+
fallbackUrl?: string;
|
|
29
|
+
forwardKeyboardEvents?: boolean;
|
|
30
|
+
restoreScroll?: boolean;
|
|
31
|
+
restoreFocus?: boolean;
|
|
32
|
+
onBeforeOpen?: () => boolean | Promise<boolean>;
|
|
33
|
+
onOpen?: (pipWindow: Window) => void;
|
|
34
|
+
onPipWindowReady?: (pipWindow: Window) => void;
|
|
35
|
+
onClose?: () => void;
|
|
36
|
+
onError?: (err: Error) => void;
|
|
37
|
+
contentEl?: HTMLElement;
|
|
38
|
+
originEl?: HTMLElement;
|
|
39
|
+
}
|
|
40
|
+
interface PipState {
|
|
41
|
+
isOpen: boolean;
|
|
42
|
+
isSupported: boolean;
|
|
43
|
+
pipWindow: Window | null;
|
|
44
|
+
}
|
|
45
|
+
interface PipInstance {
|
|
46
|
+
id: string;
|
|
47
|
+
open: (elements?: {
|
|
48
|
+
contentEl?: HTMLElement;
|
|
49
|
+
originEl?: HTMLElement;
|
|
50
|
+
}) => Promise<void>;
|
|
51
|
+
close: () => void;
|
|
52
|
+
toggle: (elements?: {
|
|
53
|
+
contentEl?: HTMLElement;
|
|
54
|
+
originEl?: HTMLElement;
|
|
55
|
+
}) => Promise<void>;
|
|
56
|
+
isOpen: () => boolean;
|
|
57
|
+
getPipWindow: () => Window | null;
|
|
58
|
+
subscribe: (fn: () => void) => () => void;
|
|
59
|
+
getState: () => PipState;
|
|
60
|
+
updateElements: (elements: {
|
|
61
|
+
contentEl?: HTMLElement;
|
|
62
|
+
originEl?: HTMLElement;
|
|
63
|
+
}) => void;
|
|
64
|
+
destroy: () => void;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
declare const createPip: (options?: PipOptions) => PipInstance;
|
|
68
|
+
|
|
69
|
+
declare const isSupported: () => boolean;
|
|
70
|
+
|
|
71
|
+
declare const registerPip: (id: string, instance: PipInstance) => void;
|
|
72
|
+
declare const unregisterPip: (id: string) => void;
|
|
73
|
+
declare const getPip: (id: string) => PipInstance | null;
|
|
74
|
+
declare const subscribeRegistry: (id: string, fn: () => void) => (() => void);
|
|
75
|
+
declare const clearRegistry: () => void;
|
|
76
|
+
|
|
77
|
+
export { type CopyStylesMode, type DomMode, type FallbackMode, type PipInstance, type PipOptions, type PipState, clearRegistry, createPip, getPip, isSupported, registerPip, subscribeRegistry, unregisterPip };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,491 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
clearRegistry: () => clearRegistry,
|
|
24
|
+
createPip: () => createPip,
|
|
25
|
+
getPip: () => getPip,
|
|
26
|
+
isSupported: () => isSupported,
|
|
27
|
+
registerPip: () => registerPip,
|
|
28
|
+
subscribeRegistry: () => subscribeRegistry,
|
|
29
|
+
unregisterPip: () => unregisterPip
|
|
30
|
+
});
|
|
31
|
+
module.exports = __toCommonJS(index_exports);
|
|
32
|
+
|
|
33
|
+
// src/support.ts
|
|
34
|
+
var isSupported = () => {
|
|
35
|
+
if (typeof window === "undefined") {
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
38
|
+
return "documentPictureInPicture" in window && window.documentPictureInPicture !== void 0;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
// src/registry.ts
|
|
42
|
+
var registry = /* @__PURE__ */ new Map();
|
|
43
|
+
var listeners = /* @__PURE__ */ new Map();
|
|
44
|
+
var registerPip = (id, instance) => {
|
|
45
|
+
registry.set(id, instance);
|
|
46
|
+
notifyListeners(id);
|
|
47
|
+
};
|
|
48
|
+
var unregisterPip = (id) => {
|
|
49
|
+
registry.delete(id);
|
|
50
|
+
notifyListeners(id);
|
|
51
|
+
};
|
|
52
|
+
var getPip = (id) => {
|
|
53
|
+
return registry.get(id) || null;
|
|
54
|
+
};
|
|
55
|
+
var subscribeRegistry = (id, fn) => {
|
|
56
|
+
let set = listeners.get(id);
|
|
57
|
+
if (!set) {
|
|
58
|
+
set = /* @__PURE__ */ new Set();
|
|
59
|
+
listeners.set(id, set);
|
|
60
|
+
}
|
|
61
|
+
set.add(fn);
|
|
62
|
+
return () => {
|
|
63
|
+
set?.delete(fn);
|
|
64
|
+
if (set?.size === 0) {
|
|
65
|
+
listeners.delete(id);
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
};
|
|
69
|
+
var notifyListeners = (id) => {
|
|
70
|
+
const set = listeners.get(id);
|
|
71
|
+
if (set) {
|
|
72
|
+
for (const fn of set) {
|
|
73
|
+
fn();
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
var clearRegistry = () => {
|
|
78
|
+
registry.clear();
|
|
79
|
+
listeners.clear();
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
// src/styles.ts
|
|
83
|
+
var copyStylesOnce = (pipWindow) => {
|
|
84
|
+
const pipDoc = pipWindow.document;
|
|
85
|
+
const openerDoc = window.document;
|
|
86
|
+
const stylesheets = Array.from(openerDoc.querySelectorAll('link[rel="stylesheet"], style'));
|
|
87
|
+
for (const sheet of stylesheets) {
|
|
88
|
+
pipDoc.head.appendChild(sheet.cloneNode(true));
|
|
89
|
+
}
|
|
90
|
+
syncAttrs(openerDoc.documentElement, pipDoc.documentElement);
|
|
91
|
+
syncAttrs(openerDoc.body, pipDoc.body);
|
|
92
|
+
};
|
|
93
|
+
var syncAttrs = (source, target) => {
|
|
94
|
+
target.className = source.className;
|
|
95
|
+
target.style.cssText = source.style.cssText;
|
|
96
|
+
for (const attr of Array.from(source.attributes)) {
|
|
97
|
+
if (attr.name.startsWith("data-")) {
|
|
98
|
+
target.setAttribute(attr.name, attr.value);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
var startStylesSync = (pipWindow) => {
|
|
103
|
+
const pipDoc = pipWindow.document;
|
|
104
|
+
const openerDoc = window.document;
|
|
105
|
+
const nodeMap = /* @__PURE__ */ new WeakMap();
|
|
106
|
+
const stylesheets = Array.from(openerDoc.querySelectorAll('link[rel="stylesheet"], style'));
|
|
107
|
+
for (const sheet of stylesheets) {
|
|
108
|
+
const clone = sheet.cloneNode(true);
|
|
109
|
+
nodeMap.set(sheet, clone);
|
|
110
|
+
pipDoc.head.appendChild(clone);
|
|
111
|
+
}
|
|
112
|
+
syncAttrs(openerDoc.documentElement, pipDoc.documentElement);
|
|
113
|
+
syncAttrs(openerDoc.body, pipDoc.body);
|
|
114
|
+
const headObserver = new MutationObserver((mutations) => {
|
|
115
|
+
for (const mutation of mutations) {
|
|
116
|
+
if (mutation.type === "childList") {
|
|
117
|
+
for (const node of Array.from(mutation.addedNodes)) {
|
|
118
|
+
if (node.nodeName === "STYLE" || node.nodeName === "LINK" && node.rel === "stylesheet") {
|
|
119
|
+
const clone = node.cloneNode(true);
|
|
120
|
+
nodeMap.set(node, clone);
|
|
121
|
+
pipDoc.head.appendChild(clone);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
for (const node of Array.from(mutation.removedNodes)) {
|
|
125
|
+
const clone = nodeMap.get(node);
|
|
126
|
+
if (clone && clone.parentNode) {
|
|
127
|
+
clone.parentNode.removeChild(clone);
|
|
128
|
+
nodeMap.delete(node);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
} else if (mutation.type === "characterData") {
|
|
132
|
+
let current = mutation.target;
|
|
133
|
+
while (current && current.nodeName !== "STYLE") {
|
|
134
|
+
current = current.parentNode;
|
|
135
|
+
}
|
|
136
|
+
if (current) {
|
|
137
|
+
const clone = nodeMap.get(current);
|
|
138
|
+
if (clone) {
|
|
139
|
+
clone.textContent = current.textContent;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
});
|
|
145
|
+
headObserver.observe(openerDoc.head, {
|
|
146
|
+
childList: true,
|
|
147
|
+
subtree: true,
|
|
148
|
+
characterData: true
|
|
149
|
+
});
|
|
150
|
+
const attrObserver = new MutationObserver((mutations) => {
|
|
151
|
+
for (const mutation of mutations) {
|
|
152
|
+
if (mutation.type === "attributes") {
|
|
153
|
+
const source = mutation.target;
|
|
154
|
+
const attrName = mutation.attributeName;
|
|
155
|
+
if (!attrName) continue;
|
|
156
|
+
let target = null;
|
|
157
|
+
if (source === openerDoc.documentElement) target = pipDoc.documentElement;
|
|
158
|
+
else if (source === openerDoc.body) target = pipDoc.body;
|
|
159
|
+
if (target) {
|
|
160
|
+
const val = source.getAttribute(attrName);
|
|
161
|
+
if (val === null) {
|
|
162
|
+
target.removeAttribute(attrName);
|
|
163
|
+
} else {
|
|
164
|
+
target.setAttribute(attrName, val);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
});
|
|
170
|
+
attrObserver.observe(openerDoc.documentElement, { attributes: true });
|
|
171
|
+
attrObserver.observe(openerDoc.body, { attributes: true });
|
|
172
|
+
return () => {
|
|
173
|
+
headObserver.disconnect();
|
|
174
|
+
attrObserver.disconnect();
|
|
175
|
+
};
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
// src/dom-modes.ts
|
|
179
|
+
var applyMoveMode = (pipWindow, contentEl, originEl) => {
|
|
180
|
+
pipWindow.document.body.appendChild(contentEl);
|
|
181
|
+
return () => {
|
|
182
|
+
if (originEl && contentEl) {
|
|
183
|
+
originEl.appendChild(contentEl);
|
|
184
|
+
}
|
|
185
|
+
};
|
|
186
|
+
};
|
|
187
|
+
var applyCloneMode = (pipWindow, contentEl) => {
|
|
188
|
+
const clone = contentEl.cloneNode(true);
|
|
189
|
+
pipWindow.document.body.appendChild(clone);
|
|
190
|
+
return () => {
|
|
191
|
+
};
|
|
192
|
+
};
|
|
193
|
+
var applyPortalAnchorMode = (pipWindow) => {
|
|
194
|
+
return () => {
|
|
195
|
+
};
|
|
196
|
+
};
|
|
197
|
+
|
|
198
|
+
// src/keyboard-bridge.ts
|
|
199
|
+
var startKeyboardBridge = (pipWindow, openerWindow = window) => {
|
|
200
|
+
const handleKey = (e) => {
|
|
201
|
+
const init = {
|
|
202
|
+
key: e.key,
|
|
203
|
+
code: e.code,
|
|
204
|
+
location: e.location,
|
|
205
|
+
ctrlKey: e.ctrlKey,
|
|
206
|
+
shiftKey: e.shiftKey,
|
|
207
|
+
altKey: e.altKey,
|
|
208
|
+
metaKey: e.metaKey,
|
|
209
|
+
repeat: e.repeat,
|
|
210
|
+
isComposing: e.isComposing,
|
|
211
|
+
bubbles: true,
|
|
212
|
+
cancelable: true,
|
|
213
|
+
composed: true
|
|
214
|
+
};
|
|
215
|
+
const cloneEvent = new KeyboardEvent(e.type, init);
|
|
216
|
+
Object.defineProperties(cloneEvent, {
|
|
217
|
+
keyCode: { get: () => e.keyCode },
|
|
218
|
+
charCode: { get: () => e.charCode },
|
|
219
|
+
which: { get: () => e.which }
|
|
220
|
+
});
|
|
221
|
+
const canceled = !openerWindow.document.dispatchEvent(cloneEvent);
|
|
222
|
+
if (canceled) {
|
|
223
|
+
e.preventDefault();
|
|
224
|
+
}
|
|
225
|
+
};
|
|
226
|
+
pipWindow.addEventListener("keydown", handleKey);
|
|
227
|
+
pipWindow.addEventListener("keyup", handleKey);
|
|
228
|
+
return () => {
|
|
229
|
+
pipWindow.removeEventListener("keydown", handleKey);
|
|
230
|
+
pipWindow.removeEventListener("keyup", handleKey);
|
|
231
|
+
};
|
|
232
|
+
};
|
|
233
|
+
|
|
234
|
+
// src/focus-scroll.ts
|
|
235
|
+
var snapshotScrollFocus = (rootEl) => {
|
|
236
|
+
const openerDoc = window.document;
|
|
237
|
+
const activeElement = openerDoc.activeElement;
|
|
238
|
+
let selectionStart = null;
|
|
239
|
+
let selectionEnd = null;
|
|
240
|
+
let selectionDir = null;
|
|
241
|
+
if (activeElement && (activeElement instanceof HTMLInputElement || activeElement instanceof HTMLTextAreaElement)) {
|
|
242
|
+
try {
|
|
243
|
+
selectionStart = activeElement.selectionStart;
|
|
244
|
+
selectionEnd = activeElement.selectionEnd;
|
|
245
|
+
selectionDir = activeElement.selectionDirection;
|
|
246
|
+
} catch {
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
const scrollMap = /* @__PURE__ */ new WeakMap();
|
|
250
|
+
const walk = (node) => {
|
|
251
|
+
if (node instanceof HTMLElement) {
|
|
252
|
+
if (node.scrollTop > 0 || node.scrollLeft > 0) {
|
|
253
|
+
scrollMap.set(node, { scrollTop: node.scrollTop, scrollLeft: node.scrollLeft });
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
for (const child of Array.from(node.children)) {
|
|
257
|
+
walk(child);
|
|
258
|
+
}
|
|
259
|
+
};
|
|
260
|
+
walk(rootEl);
|
|
261
|
+
return {
|
|
262
|
+
restore: () => {
|
|
263
|
+
const restoreWalk = (node) => {
|
|
264
|
+
if (node instanceof HTMLElement) {
|
|
265
|
+
const state = scrollMap.get(node);
|
|
266
|
+
if (state) {
|
|
267
|
+
node.scrollTop = state.scrollTop;
|
|
268
|
+
node.scrollLeft = state.scrollLeft;
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
for (const child of Array.from(node.children)) {
|
|
272
|
+
restoreWalk(child);
|
|
273
|
+
}
|
|
274
|
+
};
|
|
275
|
+
restoreWalk(rootEl);
|
|
276
|
+
if (activeElement && openerDoc.body.contains(activeElement)) {
|
|
277
|
+
activeElement.focus({ preventScroll: true });
|
|
278
|
+
if (selectionStart !== null && selectionEnd !== null) {
|
|
279
|
+
try {
|
|
280
|
+
activeElement.setSelectionRange(
|
|
281
|
+
selectionStart,
|
|
282
|
+
selectionEnd,
|
|
283
|
+
selectionDir || "none"
|
|
284
|
+
);
|
|
285
|
+
} catch {
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
};
|
|
291
|
+
};
|
|
292
|
+
|
|
293
|
+
// src/fixed-size.ts
|
|
294
|
+
var attachFixedSizeGuard = (pipWindow, width, height) => {
|
|
295
|
+
let isResizing = false;
|
|
296
|
+
const handleResize = () => {
|
|
297
|
+
if (isResizing) return;
|
|
298
|
+
if (pipWindow.innerWidth !== width || pipWindow.innerHeight !== height) {
|
|
299
|
+
isResizing = true;
|
|
300
|
+
try {
|
|
301
|
+
pipWindow.resizeTo(width, height);
|
|
302
|
+
} catch {
|
|
303
|
+
}
|
|
304
|
+
setTimeout(() => {
|
|
305
|
+
isResizing = false;
|
|
306
|
+
}, 50);
|
|
307
|
+
}
|
|
308
|
+
};
|
|
309
|
+
pipWindow.addEventListener("resize", handleResize);
|
|
310
|
+
return () => {
|
|
311
|
+
pipWindow.removeEventListener("resize", handleResize);
|
|
312
|
+
};
|
|
313
|
+
};
|
|
314
|
+
|
|
315
|
+
// src/fallback.ts
|
|
316
|
+
var executeFallback = (fallback, options, contentEl, originEl) => {
|
|
317
|
+
if (typeof fallback === "function") {
|
|
318
|
+
fallback({ contentEl, originEl, options });
|
|
319
|
+
return;
|
|
320
|
+
}
|
|
321
|
+
switch (fallback) {
|
|
322
|
+
case "new-tab":
|
|
323
|
+
if (options.fallbackUrl) {
|
|
324
|
+
window.open(options.fallbackUrl, "_blank");
|
|
325
|
+
} else {
|
|
326
|
+
console.warn('pip-it-up: fallback="new-tab" requires fallbackUrl option');
|
|
327
|
+
}
|
|
328
|
+
break;
|
|
329
|
+
case "modal":
|
|
330
|
+
break;
|
|
331
|
+
case "none":
|
|
332
|
+
console.warn("pip-it-up: Document Picture-in-Picture is not supported in this browser.");
|
|
333
|
+
break;
|
|
334
|
+
}
|
|
335
|
+
};
|
|
336
|
+
|
|
337
|
+
// src/createPip.ts
|
|
338
|
+
var idCounter = 0;
|
|
339
|
+
var createPip = (options = {}) => {
|
|
340
|
+
const id = options.id || `pip-instance-${++idCounter}`;
|
|
341
|
+
let state = {
|
|
342
|
+
isOpen: false,
|
|
343
|
+
isSupported: isSupported(),
|
|
344
|
+
pipWindow: null
|
|
345
|
+
};
|
|
346
|
+
const listeners2 = /* @__PURE__ */ new Set();
|
|
347
|
+
const disposers = [];
|
|
348
|
+
const updateState = (newState) => {
|
|
349
|
+
state = { ...state, ...newState };
|
|
350
|
+
listeners2.forEach((fn) => fn());
|
|
351
|
+
};
|
|
352
|
+
const cleanup = () => {
|
|
353
|
+
while (disposers.length > 0) {
|
|
354
|
+
const dispose = disposers.pop();
|
|
355
|
+
if (dispose) dispose();
|
|
356
|
+
}
|
|
357
|
+
};
|
|
358
|
+
const close = () => {
|
|
359
|
+
if (!state.isOpen) return;
|
|
360
|
+
if (state.pipWindow && !state.pipWindow.closed) {
|
|
361
|
+
state.pipWindow.close();
|
|
362
|
+
}
|
|
363
|
+
cleanup();
|
|
364
|
+
updateState({ isOpen: false, pipWindow: null });
|
|
365
|
+
if (options.onClose) {
|
|
366
|
+
options.onClose();
|
|
367
|
+
}
|
|
368
|
+
};
|
|
369
|
+
const open = async (elements) => {
|
|
370
|
+
if (state.isOpen) return;
|
|
371
|
+
const contentEl = elements?.contentEl || options.contentEl;
|
|
372
|
+
const originEl = elements?.originEl || options.originEl;
|
|
373
|
+
const mode = options.mode || "move";
|
|
374
|
+
if (!state.isSupported) {
|
|
375
|
+
const fallback = options.fallback || "none";
|
|
376
|
+
executeFallback(fallback, options, contentEl, originEl);
|
|
377
|
+
if (fallback === "modal") {
|
|
378
|
+
updateState({ isOpen: true, pipWindow: null });
|
|
379
|
+
if (options.onOpen) options.onOpen(window);
|
|
380
|
+
}
|
|
381
|
+
return;
|
|
382
|
+
}
|
|
383
|
+
try {
|
|
384
|
+
if (options.onBeforeOpen) {
|
|
385
|
+
const shouldOpen = await options.onBeforeOpen();
|
|
386
|
+
if (shouldOpen === false) return;
|
|
387
|
+
}
|
|
388
|
+
let restoreFocusScroll = null;
|
|
389
|
+
if (options.restoreScroll !== false && options.restoreFocus !== false && contentEl) {
|
|
390
|
+
const snap = snapshotScrollFocus(contentEl);
|
|
391
|
+
restoreFocusScroll = snap.restore;
|
|
392
|
+
}
|
|
393
|
+
const width = options.width || 900;
|
|
394
|
+
const height = options.height || 600;
|
|
395
|
+
const lockAspectRatio = options.lockAspectRatio || options.fixedSize || false;
|
|
396
|
+
const pipReqOpts = {
|
|
397
|
+
width,
|
|
398
|
+
height,
|
|
399
|
+
disallowReturnToOpener: options.disallowReturnToOpener,
|
|
400
|
+
preferInitialWindowPlacement: options.preferInitialWindowPlacement,
|
|
401
|
+
...lockAspectRatio ? { lockAspectRatio: true } : {}
|
|
402
|
+
};
|
|
403
|
+
const pipWindow = await window.documentPictureInPicture.requestWindow(pipReqOpts);
|
|
404
|
+
pipWindow.addEventListener("pagehide", () => {
|
|
405
|
+
close();
|
|
406
|
+
});
|
|
407
|
+
const copyMode = options.copyStyles || "sync";
|
|
408
|
+
if (copyMode === "sync") {
|
|
409
|
+
disposers.push(startStylesSync(pipWindow));
|
|
410
|
+
} else {
|
|
411
|
+
copyStylesOnce(pipWindow);
|
|
412
|
+
}
|
|
413
|
+
if (contentEl && originEl && mode === "move") {
|
|
414
|
+
disposers.push(applyMoveMode(pipWindow, contentEl, originEl));
|
|
415
|
+
} else if (contentEl && mode === "clone") {
|
|
416
|
+
disposers.push(applyCloneMode(pipWindow, contentEl));
|
|
417
|
+
} else if (mode === "portal") {
|
|
418
|
+
disposers.push(applyPortalAnchorMode(pipWindow));
|
|
419
|
+
}
|
|
420
|
+
if (options.forwardKeyboardEvents !== false) {
|
|
421
|
+
disposers.push(startKeyboardBridge(pipWindow, window));
|
|
422
|
+
}
|
|
423
|
+
if (options.fixedSize) {
|
|
424
|
+
disposers.push(attachFixedSizeGuard(pipWindow, width, height));
|
|
425
|
+
}
|
|
426
|
+
if (restoreFocusScroll) {
|
|
427
|
+
disposers.push(restoreFocusScroll);
|
|
428
|
+
}
|
|
429
|
+
updateState({ isOpen: true, pipWindow });
|
|
430
|
+
if (options.onOpen) {
|
|
431
|
+
options.onOpen(pipWindow);
|
|
432
|
+
}
|
|
433
|
+
requestAnimationFrame(() => {
|
|
434
|
+
if (options.onPipWindowReady) {
|
|
435
|
+
options.onPipWindowReady(pipWindow);
|
|
436
|
+
}
|
|
437
|
+
});
|
|
438
|
+
} catch (err) {
|
|
439
|
+
cleanup();
|
|
440
|
+
updateState({ isOpen: false, pipWindow: null });
|
|
441
|
+
if (options.onError) {
|
|
442
|
+
options.onError(err);
|
|
443
|
+
} else {
|
|
444
|
+
throw err;
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
};
|
|
448
|
+
const toggle = async (elements) => {
|
|
449
|
+
if (state.isOpen) {
|
|
450
|
+
close();
|
|
451
|
+
} else {
|
|
452
|
+
await open(elements);
|
|
453
|
+
}
|
|
454
|
+
};
|
|
455
|
+
const instance = {
|
|
456
|
+
id,
|
|
457
|
+
open,
|
|
458
|
+
close,
|
|
459
|
+
toggle,
|
|
460
|
+
isOpen: () => state.isOpen,
|
|
461
|
+
getPipWindow: () => state.pipWindow,
|
|
462
|
+
subscribe: (fn) => {
|
|
463
|
+
listeners2.add(fn);
|
|
464
|
+
return () => listeners2.delete(fn);
|
|
465
|
+
},
|
|
466
|
+
getState: () => state,
|
|
467
|
+
updateElements: (elements) => {
|
|
468
|
+
if (elements.contentEl !== void 0) options.contentEl = elements.contentEl;
|
|
469
|
+
if (elements.originEl !== void 0) options.originEl = elements.originEl;
|
|
470
|
+
},
|
|
471
|
+
destroy: () => {
|
|
472
|
+
close();
|
|
473
|
+
unregisterPip(id);
|
|
474
|
+
listeners2.clear();
|
|
475
|
+
}
|
|
476
|
+
};
|
|
477
|
+
if (options.id) {
|
|
478
|
+
registerPip(id, instance);
|
|
479
|
+
}
|
|
480
|
+
return instance;
|
|
481
|
+
};
|
|
482
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
483
|
+
0 && (module.exports = {
|
|
484
|
+
clearRegistry,
|
|
485
|
+
createPip,
|
|
486
|
+
getPip,
|
|
487
|
+
isSupported,
|
|
488
|
+
registerPip,
|
|
489
|
+
subscribeRegistry,
|
|
490
|
+
unregisterPip
|
|
491
|
+
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,458 @@
|
|
|
1
|
+
// src/support.ts
|
|
2
|
+
var isSupported = () => {
|
|
3
|
+
if (typeof window === "undefined") {
|
|
4
|
+
return false;
|
|
5
|
+
}
|
|
6
|
+
return "documentPictureInPicture" in window && window.documentPictureInPicture !== void 0;
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
// src/registry.ts
|
|
10
|
+
var registry = /* @__PURE__ */ new Map();
|
|
11
|
+
var listeners = /* @__PURE__ */ new Map();
|
|
12
|
+
var registerPip = (id, instance) => {
|
|
13
|
+
registry.set(id, instance);
|
|
14
|
+
notifyListeners(id);
|
|
15
|
+
};
|
|
16
|
+
var unregisterPip = (id) => {
|
|
17
|
+
registry.delete(id);
|
|
18
|
+
notifyListeners(id);
|
|
19
|
+
};
|
|
20
|
+
var getPip = (id) => {
|
|
21
|
+
return registry.get(id) || null;
|
|
22
|
+
};
|
|
23
|
+
var subscribeRegistry = (id, fn) => {
|
|
24
|
+
let set = listeners.get(id);
|
|
25
|
+
if (!set) {
|
|
26
|
+
set = /* @__PURE__ */ new Set();
|
|
27
|
+
listeners.set(id, set);
|
|
28
|
+
}
|
|
29
|
+
set.add(fn);
|
|
30
|
+
return () => {
|
|
31
|
+
set?.delete(fn);
|
|
32
|
+
if (set?.size === 0) {
|
|
33
|
+
listeners.delete(id);
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
};
|
|
37
|
+
var notifyListeners = (id) => {
|
|
38
|
+
const set = listeners.get(id);
|
|
39
|
+
if (set) {
|
|
40
|
+
for (const fn of set) {
|
|
41
|
+
fn();
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
var clearRegistry = () => {
|
|
46
|
+
registry.clear();
|
|
47
|
+
listeners.clear();
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
// src/styles.ts
|
|
51
|
+
var copyStylesOnce = (pipWindow) => {
|
|
52
|
+
const pipDoc = pipWindow.document;
|
|
53
|
+
const openerDoc = window.document;
|
|
54
|
+
const stylesheets = Array.from(openerDoc.querySelectorAll('link[rel="stylesheet"], style'));
|
|
55
|
+
for (const sheet of stylesheets) {
|
|
56
|
+
pipDoc.head.appendChild(sheet.cloneNode(true));
|
|
57
|
+
}
|
|
58
|
+
syncAttrs(openerDoc.documentElement, pipDoc.documentElement);
|
|
59
|
+
syncAttrs(openerDoc.body, pipDoc.body);
|
|
60
|
+
};
|
|
61
|
+
var syncAttrs = (source, target) => {
|
|
62
|
+
target.className = source.className;
|
|
63
|
+
target.style.cssText = source.style.cssText;
|
|
64
|
+
for (const attr of Array.from(source.attributes)) {
|
|
65
|
+
if (attr.name.startsWith("data-")) {
|
|
66
|
+
target.setAttribute(attr.name, attr.value);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
var startStylesSync = (pipWindow) => {
|
|
71
|
+
const pipDoc = pipWindow.document;
|
|
72
|
+
const openerDoc = window.document;
|
|
73
|
+
const nodeMap = /* @__PURE__ */ new WeakMap();
|
|
74
|
+
const stylesheets = Array.from(openerDoc.querySelectorAll('link[rel="stylesheet"], style'));
|
|
75
|
+
for (const sheet of stylesheets) {
|
|
76
|
+
const clone = sheet.cloneNode(true);
|
|
77
|
+
nodeMap.set(sheet, clone);
|
|
78
|
+
pipDoc.head.appendChild(clone);
|
|
79
|
+
}
|
|
80
|
+
syncAttrs(openerDoc.documentElement, pipDoc.documentElement);
|
|
81
|
+
syncAttrs(openerDoc.body, pipDoc.body);
|
|
82
|
+
const headObserver = new MutationObserver((mutations) => {
|
|
83
|
+
for (const mutation of mutations) {
|
|
84
|
+
if (mutation.type === "childList") {
|
|
85
|
+
for (const node of Array.from(mutation.addedNodes)) {
|
|
86
|
+
if (node.nodeName === "STYLE" || node.nodeName === "LINK" && node.rel === "stylesheet") {
|
|
87
|
+
const clone = node.cloneNode(true);
|
|
88
|
+
nodeMap.set(node, clone);
|
|
89
|
+
pipDoc.head.appendChild(clone);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
for (const node of Array.from(mutation.removedNodes)) {
|
|
93
|
+
const clone = nodeMap.get(node);
|
|
94
|
+
if (clone && clone.parentNode) {
|
|
95
|
+
clone.parentNode.removeChild(clone);
|
|
96
|
+
nodeMap.delete(node);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
} else if (mutation.type === "characterData") {
|
|
100
|
+
let current = mutation.target;
|
|
101
|
+
while (current && current.nodeName !== "STYLE") {
|
|
102
|
+
current = current.parentNode;
|
|
103
|
+
}
|
|
104
|
+
if (current) {
|
|
105
|
+
const clone = nodeMap.get(current);
|
|
106
|
+
if (clone) {
|
|
107
|
+
clone.textContent = current.textContent;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
});
|
|
113
|
+
headObserver.observe(openerDoc.head, {
|
|
114
|
+
childList: true,
|
|
115
|
+
subtree: true,
|
|
116
|
+
characterData: true
|
|
117
|
+
});
|
|
118
|
+
const attrObserver = new MutationObserver((mutations) => {
|
|
119
|
+
for (const mutation of mutations) {
|
|
120
|
+
if (mutation.type === "attributes") {
|
|
121
|
+
const source = mutation.target;
|
|
122
|
+
const attrName = mutation.attributeName;
|
|
123
|
+
if (!attrName) continue;
|
|
124
|
+
let target = null;
|
|
125
|
+
if (source === openerDoc.documentElement) target = pipDoc.documentElement;
|
|
126
|
+
else if (source === openerDoc.body) target = pipDoc.body;
|
|
127
|
+
if (target) {
|
|
128
|
+
const val = source.getAttribute(attrName);
|
|
129
|
+
if (val === null) {
|
|
130
|
+
target.removeAttribute(attrName);
|
|
131
|
+
} else {
|
|
132
|
+
target.setAttribute(attrName, val);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
});
|
|
138
|
+
attrObserver.observe(openerDoc.documentElement, { attributes: true });
|
|
139
|
+
attrObserver.observe(openerDoc.body, { attributes: true });
|
|
140
|
+
return () => {
|
|
141
|
+
headObserver.disconnect();
|
|
142
|
+
attrObserver.disconnect();
|
|
143
|
+
};
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
// src/dom-modes.ts
|
|
147
|
+
var applyMoveMode = (pipWindow, contentEl, originEl) => {
|
|
148
|
+
pipWindow.document.body.appendChild(contentEl);
|
|
149
|
+
return () => {
|
|
150
|
+
if (originEl && contentEl) {
|
|
151
|
+
originEl.appendChild(contentEl);
|
|
152
|
+
}
|
|
153
|
+
};
|
|
154
|
+
};
|
|
155
|
+
var applyCloneMode = (pipWindow, contentEl) => {
|
|
156
|
+
const clone = contentEl.cloneNode(true);
|
|
157
|
+
pipWindow.document.body.appendChild(clone);
|
|
158
|
+
return () => {
|
|
159
|
+
};
|
|
160
|
+
};
|
|
161
|
+
var applyPortalAnchorMode = (pipWindow) => {
|
|
162
|
+
return () => {
|
|
163
|
+
};
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
// src/keyboard-bridge.ts
|
|
167
|
+
var startKeyboardBridge = (pipWindow, openerWindow = window) => {
|
|
168
|
+
const handleKey = (e) => {
|
|
169
|
+
const init = {
|
|
170
|
+
key: e.key,
|
|
171
|
+
code: e.code,
|
|
172
|
+
location: e.location,
|
|
173
|
+
ctrlKey: e.ctrlKey,
|
|
174
|
+
shiftKey: e.shiftKey,
|
|
175
|
+
altKey: e.altKey,
|
|
176
|
+
metaKey: e.metaKey,
|
|
177
|
+
repeat: e.repeat,
|
|
178
|
+
isComposing: e.isComposing,
|
|
179
|
+
bubbles: true,
|
|
180
|
+
cancelable: true,
|
|
181
|
+
composed: true
|
|
182
|
+
};
|
|
183
|
+
const cloneEvent = new KeyboardEvent(e.type, init);
|
|
184
|
+
Object.defineProperties(cloneEvent, {
|
|
185
|
+
keyCode: { get: () => e.keyCode },
|
|
186
|
+
charCode: { get: () => e.charCode },
|
|
187
|
+
which: { get: () => e.which }
|
|
188
|
+
});
|
|
189
|
+
const canceled = !openerWindow.document.dispatchEvent(cloneEvent);
|
|
190
|
+
if (canceled) {
|
|
191
|
+
e.preventDefault();
|
|
192
|
+
}
|
|
193
|
+
};
|
|
194
|
+
pipWindow.addEventListener("keydown", handleKey);
|
|
195
|
+
pipWindow.addEventListener("keyup", handleKey);
|
|
196
|
+
return () => {
|
|
197
|
+
pipWindow.removeEventListener("keydown", handleKey);
|
|
198
|
+
pipWindow.removeEventListener("keyup", handleKey);
|
|
199
|
+
};
|
|
200
|
+
};
|
|
201
|
+
|
|
202
|
+
// src/focus-scroll.ts
|
|
203
|
+
var snapshotScrollFocus = (rootEl) => {
|
|
204
|
+
const openerDoc = window.document;
|
|
205
|
+
const activeElement = openerDoc.activeElement;
|
|
206
|
+
let selectionStart = null;
|
|
207
|
+
let selectionEnd = null;
|
|
208
|
+
let selectionDir = null;
|
|
209
|
+
if (activeElement && (activeElement instanceof HTMLInputElement || activeElement instanceof HTMLTextAreaElement)) {
|
|
210
|
+
try {
|
|
211
|
+
selectionStart = activeElement.selectionStart;
|
|
212
|
+
selectionEnd = activeElement.selectionEnd;
|
|
213
|
+
selectionDir = activeElement.selectionDirection;
|
|
214
|
+
} catch {
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
const scrollMap = /* @__PURE__ */ new WeakMap();
|
|
218
|
+
const walk = (node) => {
|
|
219
|
+
if (node instanceof HTMLElement) {
|
|
220
|
+
if (node.scrollTop > 0 || node.scrollLeft > 0) {
|
|
221
|
+
scrollMap.set(node, { scrollTop: node.scrollTop, scrollLeft: node.scrollLeft });
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
for (const child of Array.from(node.children)) {
|
|
225
|
+
walk(child);
|
|
226
|
+
}
|
|
227
|
+
};
|
|
228
|
+
walk(rootEl);
|
|
229
|
+
return {
|
|
230
|
+
restore: () => {
|
|
231
|
+
const restoreWalk = (node) => {
|
|
232
|
+
if (node instanceof HTMLElement) {
|
|
233
|
+
const state = scrollMap.get(node);
|
|
234
|
+
if (state) {
|
|
235
|
+
node.scrollTop = state.scrollTop;
|
|
236
|
+
node.scrollLeft = state.scrollLeft;
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
for (const child of Array.from(node.children)) {
|
|
240
|
+
restoreWalk(child);
|
|
241
|
+
}
|
|
242
|
+
};
|
|
243
|
+
restoreWalk(rootEl);
|
|
244
|
+
if (activeElement && openerDoc.body.contains(activeElement)) {
|
|
245
|
+
activeElement.focus({ preventScroll: true });
|
|
246
|
+
if (selectionStart !== null && selectionEnd !== null) {
|
|
247
|
+
try {
|
|
248
|
+
activeElement.setSelectionRange(
|
|
249
|
+
selectionStart,
|
|
250
|
+
selectionEnd,
|
|
251
|
+
selectionDir || "none"
|
|
252
|
+
);
|
|
253
|
+
} catch {
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
};
|
|
259
|
+
};
|
|
260
|
+
|
|
261
|
+
// src/fixed-size.ts
|
|
262
|
+
var attachFixedSizeGuard = (pipWindow, width, height) => {
|
|
263
|
+
let isResizing = false;
|
|
264
|
+
const handleResize = () => {
|
|
265
|
+
if (isResizing) return;
|
|
266
|
+
if (pipWindow.innerWidth !== width || pipWindow.innerHeight !== height) {
|
|
267
|
+
isResizing = true;
|
|
268
|
+
try {
|
|
269
|
+
pipWindow.resizeTo(width, height);
|
|
270
|
+
} catch {
|
|
271
|
+
}
|
|
272
|
+
setTimeout(() => {
|
|
273
|
+
isResizing = false;
|
|
274
|
+
}, 50);
|
|
275
|
+
}
|
|
276
|
+
};
|
|
277
|
+
pipWindow.addEventListener("resize", handleResize);
|
|
278
|
+
return () => {
|
|
279
|
+
pipWindow.removeEventListener("resize", handleResize);
|
|
280
|
+
};
|
|
281
|
+
};
|
|
282
|
+
|
|
283
|
+
// src/fallback.ts
|
|
284
|
+
var executeFallback = (fallback, options, contentEl, originEl) => {
|
|
285
|
+
if (typeof fallback === "function") {
|
|
286
|
+
fallback({ contentEl, originEl, options });
|
|
287
|
+
return;
|
|
288
|
+
}
|
|
289
|
+
switch (fallback) {
|
|
290
|
+
case "new-tab":
|
|
291
|
+
if (options.fallbackUrl) {
|
|
292
|
+
window.open(options.fallbackUrl, "_blank");
|
|
293
|
+
} else {
|
|
294
|
+
console.warn('pip-it-up: fallback="new-tab" requires fallbackUrl option');
|
|
295
|
+
}
|
|
296
|
+
break;
|
|
297
|
+
case "modal":
|
|
298
|
+
break;
|
|
299
|
+
case "none":
|
|
300
|
+
console.warn("pip-it-up: Document Picture-in-Picture is not supported in this browser.");
|
|
301
|
+
break;
|
|
302
|
+
}
|
|
303
|
+
};
|
|
304
|
+
|
|
305
|
+
// src/createPip.ts
|
|
306
|
+
var idCounter = 0;
|
|
307
|
+
var createPip = (options = {}) => {
|
|
308
|
+
const id = options.id || `pip-instance-${++idCounter}`;
|
|
309
|
+
let state = {
|
|
310
|
+
isOpen: false,
|
|
311
|
+
isSupported: isSupported(),
|
|
312
|
+
pipWindow: null
|
|
313
|
+
};
|
|
314
|
+
const listeners2 = /* @__PURE__ */ new Set();
|
|
315
|
+
const disposers = [];
|
|
316
|
+
const updateState = (newState) => {
|
|
317
|
+
state = { ...state, ...newState };
|
|
318
|
+
listeners2.forEach((fn) => fn());
|
|
319
|
+
};
|
|
320
|
+
const cleanup = () => {
|
|
321
|
+
while (disposers.length > 0) {
|
|
322
|
+
const dispose = disposers.pop();
|
|
323
|
+
if (dispose) dispose();
|
|
324
|
+
}
|
|
325
|
+
};
|
|
326
|
+
const close = () => {
|
|
327
|
+
if (!state.isOpen) return;
|
|
328
|
+
if (state.pipWindow && !state.pipWindow.closed) {
|
|
329
|
+
state.pipWindow.close();
|
|
330
|
+
}
|
|
331
|
+
cleanup();
|
|
332
|
+
updateState({ isOpen: false, pipWindow: null });
|
|
333
|
+
if (options.onClose) {
|
|
334
|
+
options.onClose();
|
|
335
|
+
}
|
|
336
|
+
};
|
|
337
|
+
const open = async (elements) => {
|
|
338
|
+
if (state.isOpen) return;
|
|
339
|
+
const contentEl = elements?.contentEl || options.contentEl;
|
|
340
|
+
const originEl = elements?.originEl || options.originEl;
|
|
341
|
+
const mode = options.mode || "move";
|
|
342
|
+
if (!state.isSupported) {
|
|
343
|
+
const fallback = options.fallback || "none";
|
|
344
|
+
executeFallback(fallback, options, contentEl, originEl);
|
|
345
|
+
if (fallback === "modal") {
|
|
346
|
+
updateState({ isOpen: true, pipWindow: null });
|
|
347
|
+
if (options.onOpen) options.onOpen(window);
|
|
348
|
+
}
|
|
349
|
+
return;
|
|
350
|
+
}
|
|
351
|
+
try {
|
|
352
|
+
if (options.onBeforeOpen) {
|
|
353
|
+
const shouldOpen = await options.onBeforeOpen();
|
|
354
|
+
if (shouldOpen === false) return;
|
|
355
|
+
}
|
|
356
|
+
let restoreFocusScroll = null;
|
|
357
|
+
if (options.restoreScroll !== false && options.restoreFocus !== false && contentEl) {
|
|
358
|
+
const snap = snapshotScrollFocus(contentEl);
|
|
359
|
+
restoreFocusScroll = snap.restore;
|
|
360
|
+
}
|
|
361
|
+
const width = options.width || 900;
|
|
362
|
+
const height = options.height || 600;
|
|
363
|
+
const lockAspectRatio = options.lockAspectRatio || options.fixedSize || false;
|
|
364
|
+
const pipReqOpts = {
|
|
365
|
+
width,
|
|
366
|
+
height,
|
|
367
|
+
disallowReturnToOpener: options.disallowReturnToOpener,
|
|
368
|
+
preferInitialWindowPlacement: options.preferInitialWindowPlacement,
|
|
369
|
+
...lockAspectRatio ? { lockAspectRatio: true } : {}
|
|
370
|
+
};
|
|
371
|
+
const pipWindow = await window.documentPictureInPicture.requestWindow(pipReqOpts);
|
|
372
|
+
pipWindow.addEventListener("pagehide", () => {
|
|
373
|
+
close();
|
|
374
|
+
});
|
|
375
|
+
const copyMode = options.copyStyles || "sync";
|
|
376
|
+
if (copyMode === "sync") {
|
|
377
|
+
disposers.push(startStylesSync(pipWindow));
|
|
378
|
+
} else {
|
|
379
|
+
copyStylesOnce(pipWindow);
|
|
380
|
+
}
|
|
381
|
+
if (contentEl && originEl && mode === "move") {
|
|
382
|
+
disposers.push(applyMoveMode(pipWindow, contentEl, originEl));
|
|
383
|
+
} else if (contentEl && mode === "clone") {
|
|
384
|
+
disposers.push(applyCloneMode(pipWindow, contentEl));
|
|
385
|
+
} else if (mode === "portal") {
|
|
386
|
+
disposers.push(applyPortalAnchorMode(pipWindow));
|
|
387
|
+
}
|
|
388
|
+
if (options.forwardKeyboardEvents !== false) {
|
|
389
|
+
disposers.push(startKeyboardBridge(pipWindow, window));
|
|
390
|
+
}
|
|
391
|
+
if (options.fixedSize) {
|
|
392
|
+
disposers.push(attachFixedSizeGuard(pipWindow, width, height));
|
|
393
|
+
}
|
|
394
|
+
if (restoreFocusScroll) {
|
|
395
|
+
disposers.push(restoreFocusScroll);
|
|
396
|
+
}
|
|
397
|
+
updateState({ isOpen: true, pipWindow });
|
|
398
|
+
if (options.onOpen) {
|
|
399
|
+
options.onOpen(pipWindow);
|
|
400
|
+
}
|
|
401
|
+
requestAnimationFrame(() => {
|
|
402
|
+
if (options.onPipWindowReady) {
|
|
403
|
+
options.onPipWindowReady(pipWindow);
|
|
404
|
+
}
|
|
405
|
+
});
|
|
406
|
+
} catch (err) {
|
|
407
|
+
cleanup();
|
|
408
|
+
updateState({ isOpen: false, pipWindow: null });
|
|
409
|
+
if (options.onError) {
|
|
410
|
+
options.onError(err);
|
|
411
|
+
} else {
|
|
412
|
+
throw err;
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
};
|
|
416
|
+
const toggle = async (elements) => {
|
|
417
|
+
if (state.isOpen) {
|
|
418
|
+
close();
|
|
419
|
+
} else {
|
|
420
|
+
await open(elements);
|
|
421
|
+
}
|
|
422
|
+
};
|
|
423
|
+
const instance = {
|
|
424
|
+
id,
|
|
425
|
+
open,
|
|
426
|
+
close,
|
|
427
|
+
toggle,
|
|
428
|
+
isOpen: () => state.isOpen,
|
|
429
|
+
getPipWindow: () => state.pipWindow,
|
|
430
|
+
subscribe: (fn) => {
|
|
431
|
+
listeners2.add(fn);
|
|
432
|
+
return () => listeners2.delete(fn);
|
|
433
|
+
},
|
|
434
|
+
getState: () => state,
|
|
435
|
+
updateElements: (elements) => {
|
|
436
|
+
if (elements.contentEl !== void 0) options.contentEl = elements.contentEl;
|
|
437
|
+
if (elements.originEl !== void 0) options.originEl = elements.originEl;
|
|
438
|
+
},
|
|
439
|
+
destroy: () => {
|
|
440
|
+
close();
|
|
441
|
+
unregisterPip(id);
|
|
442
|
+
listeners2.clear();
|
|
443
|
+
}
|
|
444
|
+
};
|
|
445
|
+
if (options.id) {
|
|
446
|
+
registerPip(id, instance);
|
|
447
|
+
}
|
|
448
|
+
return instance;
|
|
449
|
+
};
|
|
450
|
+
export {
|
|
451
|
+
clearRegistry,
|
|
452
|
+
createPip,
|
|
453
|
+
getPip,
|
|
454
|
+
isSupported,
|
|
455
|
+
registerPip,
|
|
456
|
+
subscribeRegistry,
|
|
457
|
+
unregisterPip
|
|
458
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@pip-it-up/core",
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"repository": {
|
|
5
|
+
"type": "git",
|
|
6
|
+
"url": "git+https://github.com/Shakya47/pip-it-up.git",
|
|
7
|
+
"directory": "packages/core"
|
|
8
|
+
},
|
|
9
|
+
"bugs": {
|
|
10
|
+
"url": "https://github.com/Shakya47/pip-it-up/issues"
|
|
11
|
+
},
|
|
12
|
+
"homepage": "https://github.com/Shakya47/pip-it-up/tree/main/packages/core#readme",
|
|
13
|
+
"main": "./dist/index.js",
|
|
14
|
+
"module": "./dist/index.mjs",
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"types": "./dist/index.d.ts",
|
|
19
|
+
"import": "./dist/index.mjs",
|
|
20
|
+
"require": "./dist/index.js"
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
"publishConfig": {
|
|
24
|
+
"access": "public",
|
|
25
|
+
"provenance": true
|
|
26
|
+
},
|
|
27
|
+
"sideEffects": false,
|
|
28
|
+
"files": [
|
|
29
|
+
"dist"
|
|
30
|
+
],
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"@testing-library/jest-dom": "^6.9.1",
|
|
33
|
+
"@testing-library/react": "^16.3.2",
|
|
34
|
+
"@testing-library/user-event": "^14.6.1",
|
|
35
|
+
"typescript": "^5.4.0",
|
|
36
|
+
"vitest": "^1.4.0"
|
|
37
|
+
},
|
|
38
|
+
"scripts": {
|
|
39
|
+
"build": "tsup",
|
|
40
|
+
"dev": "tsup --watch",
|
|
41
|
+
"lint": "eslint \"src/**/*.ts\" \"tests/**/*.ts\"",
|
|
42
|
+
"typecheck": "tsc --noEmit",
|
|
43
|
+
"test": "vitest run",
|
|
44
|
+
"clean": "rm -rf dist"
|
|
45
|
+
}
|
|
46
|
+
}
|