@ansight/capacitor-pencil 0.0.0-stage → 1.9.0
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/AnsightCapacitorPencil.podspec +14 -0
- package/LICENSE +166 -0
- package/Package.swift +38 -0
- package/README.md +6 -2
- package/dist/cjs/index.d.ts +20 -0
- package/dist/cjs/index.js +118 -0
- package/dist/esm/index.d.ts +20 -0
- package/dist/esm/index.js +115 -0
- package/ios/Sources/AnsightCapacitorPencilPlugin/AnsightCapacitorPencilPlugin.swift +64 -0
- package/package.json +47 -4
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
Pod::Spec.new do |s|
|
|
2
|
+
s.name = 'AnsightCapacitorPencil'
|
|
3
|
+
s.version = '1.9.0'
|
|
4
|
+
s.summary = 'Optional Ansight Pencil simulation for Capacitor on iOS Simulator.'
|
|
5
|
+
s.license = { :type => 'PolyForm Shield 1.0.0', :file => 'LICENSE' }
|
|
6
|
+
s.homepage = 'https://github.com/ansight-ai/ansight-sdk'
|
|
7
|
+
s.author = { 'Ansight' => 'hello@ansight.ai' }
|
|
8
|
+
s.source = { :git => 'https://github.com/ansight-ai/ansight-sdk.git', :tag => s.version.to_s }
|
|
9
|
+
s.source_files = 'ios/Sources/AnsightCapacitorPencilPlugin/**/*.{swift,h,m,c,cc,mm,cpp}'
|
|
10
|
+
s.ios.deployment_target = '15.0'
|
|
11
|
+
s.swift_version = '5.9'
|
|
12
|
+
s.dependency 'Capacitor'
|
|
13
|
+
s.dependency 'AnsightPencil', s.version.to_s
|
|
14
|
+
end
|
package/LICENSE
ADDED
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
# PolyForm Shield License 1.0.0
|
|
2
|
+
|
|
3
|
+
<https://polyformproject.org/licenses/shield/1.0.0>
|
|
4
|
+
|
|
5
|
+
## Acceptance
|
|
6
|
+
|
|
7
|
+
In order to get any license under these terms, you must agree
|
|
8
|
+
to them as both strict obligations and conditions to all
|
|
9
|
+
your licenses.
|
|
10
|
+
|
|
11
|
+
## Copyright License
|
|
12
|
+
|
|
13
|
+
The licensor grants you a copyright license for the
|
|
14
|
+
software to do everything you might do with the software
|
|
15
|
+
that would otherwise infringe the licensor's copyright
|
|
16
|
+
in it for any permitted purpose. However, you may
|
|
17
|
+
only distribute the software according to [Distribution
|
|
18
|
+
License](#distribution-license) and make changes or new works
|
|
19
|
+
based on the software according to [Changes and New Works
|
|
20
|
+
License](#changes-and-new-works-license).
|
|
21
|
+
|
|
22
|
+
## Distribution License
|
|
23
|
+
|
|
24
|
+
The licensor grants you an additional copyright license
|
|
25
|
+
to distribute copies of the software. Your license
|
|
26
|
+
to distribute covers distributing the software with
|
|
27
|
+
changes and new works permitted by [Changes and New Works
|
|
28
|
+
License](#changes-and-new-works-license).
|
|
29
|
+
|
|
30
|
+
## Notices
|
|
31
|
+
|
|
32
|
+
You must ensure that anyone who gets a copy of any part of
|
|
33
|
+
the software from you also gets a copy of these terms or the
|
|
34
|
+
URL for them above, as well as copies of any plain-text lines
|
|
35
|
+
beginning with `Required Notice:` that the licensor provided
|
|
36
|
+
with the software. For example:
|
|
37
|
+
|
|
38
|
+
> Required Notice: Copyright Yoyodyne, Inc. (http://example.com)
|
|
39
|
+
|
|
40
|
+
## Changes and New Works License
|
|
41
|
+
|
|
42
|
+
The licensor grants you an additional copyright license to
|
|
43
|
+
make changes and new works based on the software for any
|
|
44
|
+
permitted purpose.
|
|
45
|
+
|
|
46
|
+
## Patent License
|
|
47
|
+
|
|
48
|
+
The licensor grants you a patent license for the software that
|
|
49
|
+
covers patent claims the licensor can license, or becomes able
|
|
50
|
+
to license, that you would infringe by using the software.
|
|
51
|
+
|
|
52
|
+
## Noncompete
|
|
53
|
+
|
|
54
|
+
Any purpose is a permitted purpose, except for providing any
|
|
55
|
+
product that competes with the software or any product the
|
|
56
|
+
licensor or any of its affiliates provides using the software.
|
|
57
|
+
|
|
58
|
+
## Competition
|
|
59
|
+
|
|
60
|
+
Goods and services compete even when they provide functionality
|
|
61
|
+
through different kinds of interfaces or for different technical
|
|
62
|
+
platforms. Applications can compete with services, libraries
|
|
63
|
+
with plugins, frameworks with development tools, and so on,
|
|
64
|
+
even if they're written in different programming languages
|
|
65
|
+
or for different computer architectures. Goods and services
|
|
66
|
+
compete even when provided free of charge. If you market a
|
|
67
|
+
product as a practical substitute for the software or another
|
|
68
|
+
product, it definitely competes.
|
|
69
|
+
|
|
70
|
+
## New Products
|
|
71
|
+
|
|
72
|
+
If you are using the software to provide a product that does
|
|
73
|
+
not compete, but the licensor or any of its affiliates brings
|
|
74
|
+
your product into competition by providing a new version of
|
|
75
|
+
the software or another product using the software, you may
|
|
76
|
+
continue using versions of the software available under these
|
|
77
|
+
terms beforehand to provide your competing product, but not
|
|
78
|
+
any later versions.
|
|
79
|
+
|
|
80
|
+
## Discontinued Products
|
|
81
|
+
|
|
82
|
+
You may begin using the software to compete with a product
|
|
83
|
+
or service that the licensor or any of its affiliates has
|
|
84
|
+
stopped providing, unless the licensor includes a plain-text
|
|
85
|
+
line beginning with `Licensor Line of Business:` with the
|
|
86
|
+
software that mentions that line of business. For example:
|
|
87
|
+
|
|
88
|
+
> Licensor Line of Business: YoyodyneCMS Content Management
|
|
89
|
+
System (http://example.com/cms)
|
|
90
|
+
|
|
91
|
+
## Sales of Business
|
|
92
|
+
|
|
93
|
+
If the licensor or any of its affiliates sells a line of
|
|
94
|
+
business developing the software or using the software
|
|
95
|
+
to provide a product, the buyer can also enforce
|
|
96
|
+
[Noncompete](#noncompete) for that product.
|
|
97
|
+
|
|
98
|
+
## Fair Use
|
|
99
|
+
|
|
100
|
+
You may have "fair use" rights for the software under the
|
|
101
|
+
law. These terms do not limit them.
|
|
102
|
+
|
|
103
|
+
## No Other Rights
|
|
104
|
+
|
|
105
|
+
These terms do not allow you to sublicense or transfer any of
|
|
106
|
+
your licenses to anyone else, or prevent the licensor from
|
|
107
|
+
granting licenses to anyone else. These terms do not imply
|
|
108
|
+
any other licenses.
|
|
109
|
+
|
|
110
|
+
## Patent Defense
|
|
111
|
+
|
|
112
|
+
If you make any written claim that the software infringes or
|
|
113
|
+
contributes to infringement of any patent, your patent license
|
|
114
|
+
for the software granted under these terms ends immediately. If
|
|
115
|
+
your company makes such a claim, your patent license ends
|
|
116
|
+
immediately for work on behalf of your company.
|
|
117
|
+
|
|
118
|
+
## Violations
|
|
119
|
+
|
|
120
|
+
The first time you are notified in writing that you have
|
|
121
|
+
violated any of these terms, or done anything with the software
|
|
122
|
+
not covered by your licenses, your licenses can nonetheless
|
|
123
|
+
continue if you come into full compliance with these terms,
|
|
124
|
+
and take practical steps to correct past violations, within
|
|
125
|
+
32 days of receiving notice. Otherwise, all your licenses
|
|
126
|
+
end immediately.
|
|
127
|
+
|
|
128
|
+
## No Liability
|
|
129
|
+
|
|
130
|
+
***As far as the law allows, the software comes as is, without
|
|
131
|
+
any warranty or condition, and the licensor will not be liable
|
|
132
|
+
to you for any damages arising out of these terms or the use
|
|
133
|
+
or nature of the software, under any kind of legal claim.***
|
|
134
|
+
|
|
135
|
+
## Definitions
|
|
136
|
+
|
|
137
|
+
The **licensor** is the individual or entity offering these
|
|
138
|
+
terms, and the **software** is the software the licensor makes
|
|
139
|
+
available under these terms.
|
|
140
|
+
|
|
141
|
+
A **product** can be a good or service, or a combination
|
|
142
|
+
of them.
|
|
143
|
+
|
|
144
|
+
**You** refers to the individual or entity agreeing to these
|
|
145
|
+
terms.
|
|
146
|
+
|
|
147
|
+
**Your company** is any legal entity, sole proprietorship,
|
|
148
|
+
or other kind of organization that you work for, plus all
|
|
149
|
+
its affiliates.
|
|
150
|
+
|
|
151
|
+
**Affiliates** means the other organizations than an
|
|
152
|
+
organization has control over, is under the control of, or is
|
|
153
|
+
under common control with.
|
|
154
|
+
|
|
155
|
+
**Control** means ownership of substantially all the assets of
|
|
156
|
+
an entity, or the power to direct its management and policies
|
|
157
|
+
by vote, contract, or otherwise. Control can be direct or
|
|
158
|
+
indirect.
|
|
159
|
+
|
|
160
|
+
**Your licenses** are all the licenses granted to you for the
|
|
161
|
+
software under these terms.
|
|
162
|
+
|
|
163
|
+
**Use** means anything you do with the software requiring one
|
|
164
|
+
of your licenses.
|
|
165
|
+
|
|
166
|
+
Required Notice: Copyright (c) 2026 Ansight, Inc. (https://www.ansight.ai)
|
package/Package.swift
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
// swift-tools-version: 5.9
|
|
2
|
+
import PackageDescription
|
|
3
|
+
import Foundation
|
|
4
|
+
|
|
5
|
+
let useLocalAnsightSdk = ProcessInfo.processInfo.environment["ANSIGHT_USE_LOCAL_SDK"] == "1"
|
|
6
|
+
let ansightSdkDependency: Package.Dependency = useLocalAnsightSdk
|
|
7
|
+
? .package(name: "AnsightSDK", path: "../ios")
|
|
8
|
+
: .package(
|
|
9
|
+
url: "https://github.com/ansight-ai/ansight-sdk.git",
|
|
10
|
+
exact: "1.9.0"
|
|
11
|
+
)
|
|
12
|
+
let ansightTargetDependency: Target.Dependency = .product(
|
|
13
|
+
name: "AnsightPencil",
|
|
14
|
+
package: useLocalAnsightSdk ? "AnsightSDK" : "ansight-sdk"
|
|
15
|
+
)
|
|
16
|
+
|
|
17
|
+
let package = Package(
|
|
18
|
+
name: "AnsightCapacitorPencil",
|
|
19
|
+
platforms: [.iOS(.v15)],
|
|
20
|
+
products: [
|
|
21
|
+
.library(name: "AnsightCapacitorPencil", targets: ["AnsightCapacitorPencilPlugin"])
|
|
22
|
+
],
|
|
23
|
+
dependencies: [
|
|
24
|
+
.package(url: "https://github.com/ionic-team/capacitor-swift-pm.git", from: "8.0.0"),
|
|
25
|
+
ansightSdkDependency
|
|
26
|
+
],
|
|
27
|
+
targets: [
|
|
28
|
+
.target(
|
|
29
|
+
name: "AnsightCapacitorPencilPlugin",
|
|
30
|
+
dependencies: [
|
|
31
|
+
.product(name: "Capacitor", package: "capacitor-swift-pm"),
|
|
32
|
+
.product(name: "Cordova", package: "capacitor-swift-pm"),
|
|
33
|
+
ansightTargetDependency
|
|
34
|
+
],
|
|
35
|
+
path: "ios/Sources/AnsightCapacitorPencilPlugin"
|
|
36
|
+
)
|
|
37
|
+
]
|
|
38
|
+
)
|
package/README.md
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @ansight/capacitor-pencil
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Optional Pencil support for iOS Simulator.
|
|
4
|
+
|
|
5
|
+
[Documentation: integration, playback, capture, compatibility and package updates](https://www.ansight.ai/docs/sdk/cordova/pencil).
|
|
6
|
+
|
|
7
|
+
These packages are currently local and unpublished.
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { PenSample } from '@ansight/pencil-runtime';
|
|
2
|
+
declare global {
|
|
3
|
+
interface PointerEvent {
|
|
4
|
+
readonly ansightPencil?: Readonly<PenSample & {
|
|
5
|
+
inputSource: string;
|
|
6
|
+
}>;
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
export declare const AnsightPencil: {
|
|
10
|
+
id: string;
|
|
11
|
+
initialize(): Promise<void>;
|
|
12
|
+
/** Bind the actual DOM element. Dispose before replacing it or leaving the screen. */
|
|
13
|
+
bindSurface(id: string, element: HTMLElement): () => Promise<void>;
|
|
14
|
+
/** Platform capability only. Does not register tools or install hooks. */
|
|
15
|
+
isSupported(): Promise<boolean>;
|
|
16
|
+
/** Low-level registration. Prefer withPlugin(AnsightPencil) in the options builder.
|
|
17
|
+
* Returns false outside iOS Simulator; rejects before initialization.
|
|
18
|
+
* Idempotent. Grants only Pencil tools; global disable still wins. Does not install hooks. */
|
|
19
|
+
register(): Promise<boolean>;
|
|
20
|
+
};
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AnsightPencil = void 0;
|
|
4
|
+
const core_1 = require("@capacitor/core");
|
|
5
|
+
const capacitor_1 = require("@ansight/capacitor");
|
|
6
|
+
const pencil_runtime_1 = require("@ansight/pencil-runtime");
|
|
7
|
+
const nativePencil = (0, core_1.registerPlugin)('AnsightPencil');
|
|
8
|
+
const runtime = (0, pencil_runtime_1.createPencilRuntime)({
|
|
9
|
+
checkPlayback: () => nativePencil.requirePlayback(),
|
|
10
|
+
capture: (sample, geometry, pointerId) => nativePencil.capture({ json: JSON.stringify({ sample, geometry, pointerId }) }),
|
|
11
|
+
});
|
|
12
|
+
let frameworkRegistration;
|
|
13
|
+
exports.AnsightPencil = {
|
|
14
|
+
id: 'ansight.pencil',
|
|
15
|
+
async initialize() {
|
|
16
|
+
if (await exports.AnsightPencil.register() && !frameworkRegistration) {
|
|
17
|
+
const registration = (0, capacitor_1.registerTool)(runtime.definition, args => runtime.handle({ command: args.command }));
|
|
18
|
+
try {
|
|
19
|
+
await registration.ready;
|
|
20
|
+
frameworkRegistration = registration;
|
|
21
|
+
}
|
|
22
|
+
catch (error) {
|
|
23
|
+
await registration.unregister();
|
|
24
|
+
throw error;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
},
|
|
28
|
+
/** Bind the actual DOM element. Dispose before replacing it or leaving the screen. */
|
|
29
|
+
bindSurface(id, element) {
|
|
30
|
+
if (!element || element.ownerDocument !== document)
|
|
31
|
+
throw new Error('Select a DOM element in the current document.');
|
|
32
|
+
const realPointers = new Set();
|
|
33
|
+
const simulated = new WeakSet();
|
|
34
|
+
let pointer;
|
|
35
|
+
let captured = false;
|
|
36
|
+
const restores = [];
|
|
37
|
+
function restoreCapture() { restores.splice(0).forEach(restore => restore()); pointer = undefined; captured = false; }
|
|
38
|
+
function installCapture(pointerId) {
|
|
39
|
+
if (pointer !== undefined)
|
|
40
|
+
return;
|
|
41
|
+
pointer = pointerId;
|
|
42
|
+
// Only this explicitly bound element is adapted; native pointers keep the browser implementation.
|
|
43
|
+
for (const key of ['setPointerCapture', 'releasePointerCapture', 'hasPointerCapture']) {
|
|
44
|
+
const descriptor = Object.getOwnPropertyDescriptor(element, key);
|
|
45
|
+
const original = element[key].bind(element);
|
|
46
|
+
const replacement = (id) => {
|
|
47
|
+
if (id !== pointer)
|
|
48
|
+
return original(id);
|
|
49
|
+
if (key === 'hasPointerCapture')
|
|
50
|
+
return captured;
|
|
51
|
+
captured = key === 'setPointerCapture';
|
|
52
|
+
};
|
|
53
|
+
Object.defineProperty(element, key, { configurable: true, value: replacement });
|
|
54
|
+
restores.push(() => {
|
|
55
|
+
if (element[key] !== replacement)
|
|
56
|
+
return;
|
|
57
|
+
if (descriptor)
|
|
58
|
+
Object.defineProperty(element, key, descriptor);
|
|
59
|
+
else
|
|
60
|
+
delete element[key];
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
const realDown = (event) => { if (!simulated.has(event) && element.contains(event.target))
|
|
65
|
+
realPointers.add(event.pointerId); };
|
|
66
|
+
const realUp = (event) => { if (!simulated.has(event))
|
|
67
|
+
realPointers.delete(event.pointerId); };
|
|
68
|
+
const events = { down: 'pointerdown', move: 'pointermove', up: 'pointerup', cancel: 'pointercancel', hoverEnter: 'pointerenter', hoverMove: 'pointermove', hoverExit: 'pointerleave' };
|
|
69
|
+
const dispose = runtime.bindSurface(id, {
|
|
70
|
+
snapshot() {
|
|
71
|
+
if (!element.isConnected || document.visibilityState === 'hidden' || (window.visualViewport?.scale ?? 1) !== 1)
|
|
72
|
+
throw new Error('Pencil surface is detached, hidden or zoomed.');
|
|
73
|
+
const rect = element.getBoundingClientRect();
|
|
74
|
+
return { x: rect.x, y: rect.y, width: rect.width, height: rect.height,
|
|
75
|
+
viewportWidth: window.innerWidth, viewportHeight: window.innerHeight, scale: window.devicePixelRatio };
|
|
76
|
+
},
|
|
77
|
+
isBusy: () => realPointers.size > 0,
|
|
78
|
+
hitTest: async (point) => {
|
|
79
|
+
const hit = document.elementFromPoint(point.x, point.y);
|
|
80
|
+
return (hit === element || (hit !== null && element.contains(hit))) &&
|
|
81
|
+
(await nativePencil.hitTest({ x: point.x, y: point.y, width: window.innerWidth, height: window.innerHeight })).value;
|
|
82
|
+
},
|
|
83
|
+
dispatch(sample, rect, pointerId) {
|
|
84
|
+
if (sample.phase === 'down')
|
|
85
|
+
installCapture(pointerId);
|
|
86
|
+
const properties = (0, pencil_runtime_1.pointerProperties)(sample, rect, pointerId);
|
|
87
|
+
const event = new PointerEvent(events[sample.phase], { ...properties,
|
|
88
|
+
bubbles: !['hoverEnter', 'hoverExit'].includes(sample.phase), cancelable: true, composed: true });
|
|
89
|
+
Object.defineProperty(event, 'ansightPencil', { value: properties.ansightPencil });
|
|
90
|
+
simulated.add(event);
|
|
91
|
+
element.dispatchEvent(event);
|
|
92
|
+
if (sample.phase === 'up' || sample.phase === 'cancel')
|
|
93
|
+
restoreCapture();
|
|
94
|
+
},
|
|
95
|
+
finish: restoreCapture,
|
|
96
|
+
onError: error => console.error('Ansight Pencil cleanup failed', error),
|
|
97
|
+
});
|
|
98
|
+
window.addEventListener('pointerdown', realDown, true);
|
|
99
|
+
window.addEventListener('pointerup', realUp, true);
|
|
100
|
+
window.addEventListener('pointercancel', realUp, true);
|
|
101
|
+
return async () => {
|
|
102
|
+
window.removeEventListener('pointerdown', realDown, true);
|
|
103
|
+
window.removeEventListener('pointerup', realUp, true);
|
|
104
|
+
window.removeEventListener('pointercancel', realUp, true);
|
|
105
|
+
await dispose();
|
|
106
|
+
};
|
|
107
|
+
},
|
|
108
|
+
/** Platform capability only. Does not register tools or install hooks. */
|
|
109
|
+
async isSupported() {
|
|
110
|
+
return core_1.Capacitor.getPlatform() === 'ios' && (await nativePencil.isSupported()).value;
|
|
111
|
+
},
|
|
112
|
+
/** Low-level registration. Prefer withPlugin(AnsightPencil) in the options builder.
|
|
113
|
+
* Returns false outside iOS Simulator; rejects before initialization.
|
|
114
|
+
* Idempotent. Grants only Pencil tools; global disable still wins. Does not install hooks. */
|
|
115
|
+
async register() {
|
|
116
|
+
return core_1.Capacitor.getPlatform() === 'ios' && (await nativePencil.register()).value;
|
|
117
|
+
},
|
|
118
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { PenSample } from '@ansight/pencil-runtime';
|
|
2
|
+
declare global {
|
|
3
|
+
interface PointerEvent {
|
|
4
|
+
readonly ansightPencil?: Readonly<PenSample & {
|
|
5
|
+
inputSource: string;
|
|
6
|
+
}>;
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
export declare const AnsightPencil: {
|
|
10
|
+
id: string;
|
|
11
|
+
initialize(): Promise<void>;
|
|
12
|
+
/** Bind the actual DOM element. Dispose before replacing it or leaving the screen. */
|
|
13
|
+
bindSurface(id: string, element: HTMLElement): () => Promise<void>;
|
|
14
|
+
/** Platform capability only. Does not register tools or install hooks. */
|
|
15
|
+
isSupported(): Promise<boolean>;
|
|
16
|
+
/** Low-level registration. Prefer withPlugin(AnsightPencil) in the options builder.
|
|
17
|
+
* Returns false outside iOS Simulator; rejects before initialization.
|
|
18
|
+
* Idempotent. Grants only Pencil tools; global disable still wins. Does not install hooks. */
|
|
19
|
+
register(): Promise<boolean>;
|
|
20
|
+
};
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { Capacitor, registerPlugin } from '@capacitor/core';
|
|
2
|
+
import { registerTool } from '@ansight/capacitor';
|
|
3
|
+
import { createPencilRuntime, pointerProperties } from '@ansight/pencil-runtime';
|
|
4
|
+
const nativePencil = registerPlugin('AnsightPencil');
|
|
5
|
+
const runtime = createPencilRuntime({
|
|
6
|
+
checkPlayback: () => nativePencil.requirePlayback(),
|
|
7
|
+
capture: (sample, geometry, pointerId) => nativePencil.capture({ json: JSON.stringify({ sample, geometry, pointerId }) }),
|
|
8
|
+
});
|
|
9
|
+
let frameworkRegistration;
|
|
10
|
+
export const AnsightPencil = {
|
|
11
|
+
id: 'ansight.pencil',
|
|
12
|
+
async initialize() {
|
|
13
|
+
if (await AnsightPencil.register() && !frameworkRegistration) {
|
|
14
|
+
const registration = registerTool(runtime.definition, args => runtime.handle({ command: args.command }));
|
|
15
|
+
try {
|
|
16
|
+
await registration.ready;
|
|
17
|
+
frameworkRegistration = registration;
|
|
18
|
+
}
|
|
19
|
+
catch (error) {
|
|
20
|
+
await registration.unregister();
|
|
21
|
+
throw error;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
/** Bind the actual DOM element. Dispose before replacing it or leaving the screen. */
|
|
26
|
+
bindSurface(id, element) {
|
|
27
|
+
if (!element || element.ownerDocument !== document)
|
|
28
|
+
throw new Error('Select a DOM element in the current document.');
|
|
29
|
+
const realPointers = new Set();
|
|
30
|
+
const simulated = new WeakSet();
|
|
31
|
+
let pointer;
|
|
32
|
+
let captured = false;
|
|
33
|
+
const restores = [];
|
|
34
|
+
function restoreCapture() { restores.splice(0).forEach(restore => restore()); pointer = undefined; captured = false; }
|
|
35
|
+
function installCapture(pointerId) {
|
|
36
|
+
if (pointer !== undefined)
|
|
37
|
+
return;
|
|
38
|
+
pointer = pointerId;
|
|
39
|
+
// Only this explicitly bound element is adapted; native pointers keep the browser implementation.
|
|
40
|
+
for (const key of ['setPointerCapture', 'releasePointerCapture', 'hasPointerCapture']) {
|
|
41
|
+
const descriptor = Object.getOwnPropertyDescriptor(element, key);
|
|
42
|
+
const original = element[key].bind(element);
|
|
43
|
+
const replacement = (id) => {
|
|
44
|
+
if (id !== pointer)
|
|
45
|
+
return original(id);
|
|
46
|
+
if (key === 'hasPointerCapture')
|
|
47
|
+
return captured;
|
|
48
|
+
captured = key === 'setPointerCapture';
|
|
49
|
+
};
|
|
50
|
+
Object.defineProperty(element, key, { configurable: true, value: replacement });
|
|
51
|
+
restores.push(() => {
|
|
52
|
+
if (element[key] !== replacement)
|
|
53
|
+
return;
|
|
54
|
+
if (descriptor)
|
|
55
|
+
Object.defineProperty(element, key, descriptor);
|
|
56
|
+
else
|
|
57
|
+
delete element[key];
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
const realDown = (event) => { if (!simulated.has(event) && element.contains(event.target))
|
|
62
|
+
realPointers.add(event.pointerId); };
|
|
63
|
+
const realUp = (event) => { if (!simulated.has(event))
|
|
64
|
+
realPointers.delete(event.pointerId); };
|
|
65
|
+
const events = { down: 'pointerdown', move: 'pointermove', up: 'pointerup', cancel: 'pointercancel', hoverEnter: 'pointerenter', hoverMove: 'pointermove', hoverExit: 'pointerleave' };
|
|
66
|
+
const dispose = runtime.bindSurface(id, {
|
|
67
|
+
snapshot() {
|
|
68
|
+
if (!element.isConnected || document.visibilityState === 'hidden' || (window.visualViewport?.scale ?? 1) !== 1)
|
|
69
|
+
throw new Error('Pencil surface is detached, hidden or zoomed.');
|
|
70
|
+
const rect = element.getBoundingClientRect();
|
|
71
|
+
return { x: rect.x, y: rect.y, width: rect.width, height: rect.height,
|
|
72
|
+
viewportWidth: window.innerWidth, viewportHeight: window.innerHeight, scale: window.devicePixelRatio };
|
|
73
|
+
},
|
|
74
|
+
isBusy: () => realPointers.size > 0,
|
|
75
|
+
hitTest: async (point) => {
|
|
76
|
+
const hit = document.elementFromPoint(point.x, point.y);
|
|
77
|
+
return (hit === element || (hit !== null && element.contains(hit))) &&
|
|
78
|
+
(await nativePencil.hitTest({ x: point.x, y: point.y, width: window.innerWidth, height: window.innerHeight })).value;
|
|
79
|
+
},
|
|
80
|
+
dispatch(sample, rect, pointerId) {
|
|
81
|
+
if (sample.phase === 'down')
|
|
82
|
+
installCapture(pointerId);
|
|
83
|
+
const properties = pointerProperties(sample, rect, pointerId);
|
|
84
|
+
const event = new PointerEvent(events[sample.phase], { ...properties,
|
|
85
|
+
bubbles: !['hoverEnter', 'hoverExit'].includes(sample.phase), cancelable: true, composed: true });
|
|
86
|
+
Object.defineProperty(event, 'ansightPencil', { value: properties.ansightPencil });
|
|
87
|
+
simulated.add(event);
|
|
88
|
+
element.dispatchEvent(event);
|
|
89
|
+
if (sample.phase === 'up' || sample.phase === 'cancel')
|
|
90
|
+
restoreCapture();
|
|
91
|
+
},
|
|
92
|
+
finish: restoreCapture,
|
|
93
|
+
onError: error => console.error('Ansight Pencil cleanup failed', error),
|
|
94
|
+
});
|
|
95
|
+
window.addEventListener('pointerdown', realDown, true);
|
|
96
|
+
window.addEventListener('pointerup', realUp, true);
|
|
97
|
+
window.addEventListener('pointercancel', realUp, true);
|
|
98
|
+
return async () => {
|
|
99
|
+
window.removeEventListener('pointerdown', realDown, true);
|
|
100
|
+
window.removeEventListener('pointerup', realUp, true);
|
|
101
|
+
window.removeEventListener('pointercancel', realUp, true);
|
|
102
|
+
await dispose();
|
|
103
|
+
};
|
|
104
|
+
},
|
|
105
|
+
/** Platform capability only. Does not register tools or install hooks. */
|
|
106
|
+
async isSupported() {
|
|
107
|
+
return Capacitor.getPlatform() === 'ios' && (await nativePencil.isSupported()).value;
|
|
108
|
+
},
|
|
109
|
+
/** Low-level registration. Prefer withPlugin(AnsightPencil) in the options builder.
|
|
110
|
+
* Returns false outside iOS Simulator; rejects before initialization.
|
|
111
|
+
* Idempotent. Grants only Pencil tools; global disable still wins. Does not install hooks. */
|
|
112
|
+
async register() {
|
|
113
|
+
return Capacitor.getPlatform() === 'ios' && (await nativePencil.register()).value;
|
|
114
|
+
},
|
|
115
|
+
};
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import AnsightPencil
|
|
2
|
+
import Capacitor
|
|
3
|
+
import UIKit
|
|
4
|
+
|
|
5
|
+
@objc(AnsightCapacitorPencilPlugin)
|
|
6
|
+
public final class AnsightCapacitorPencilPlugin: CAPPlugin, CAPBridgedPlugin {
|
|
7
|
+
public let identifier = "AnsightCapacitorPencilPlugin"
|
|
8
|
+
public let jsName = "AnsightPencil"
|
|
9
|
+
public let pluginMethods: [CAPPluginMethod] = [
|
|
10
|
+
CAPPluginMethod(name: "isSupported", returnType: CAPPluginReturnPromise),
|
|
11
|
+
CAPPluginMethod(name: "register", returnType: CAPPluginReturnPromise),
|
|
12
|
+
CAPPluginMethod(name: "requirePlayback", returnType: CAPPluginReturnPromise),
|
|
13
|
+
CAPPluginMethod(name: "capture", returnType: CAPPluginReturnPromise),
|
|
14
|
+
CAPPluginMethod(name: "hitTest", returnType: CAPPluginReturnPromise),
|
|
15
|
+
]
|
|
16
|
+
|
|
17
|
+
@objc public func isSupported(_ call: CAPPluginCall) {
|
|
18
|
+
call.resolve(["value": AnsightPencil.isSupported])
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
@objc public func requirePlayback(_ call: CAPPluginCall) {
|
|
22
|
+
do { try AnsightPencil.requireFrameworkPlayback(); call.resolve() }
|
|
23
|
+
catch { call.reject(error.localizedDescription, "pen_playback_unavailable", error) }
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
@objc public func capture(_ call: CAPPluginCall) {
|
|
27
|
+
DispatchQueue.main.async {
|
|
28
|
+
do {
|
|
29
|
+
guard let webView = self.bridge?.webView, let window = webView.window,
|
|
30
|
+
let data = call.getString("json")?.data(using: .utf8),
|
|
31
|
+
var payload = try JSONSerialization.jsonObject(with: data) as? [String: Any],
|
|
32
|
+
var sample = payload["sample"] as? [String: Any],
|
|
33
|
+
var geometry = payload["geometry"] as? [String: Any],
|
|
34
|
+
let x = sample["x"] as? Double, let y = sample["y"] as? Double else {
|
|
35
|
+
call.reject("The WebView is unavailable.", "pen_capture_failed"); return
|
|
36
|
+
}
|
|
37
|
+
let point = webView.convert(CGPoint(x: x * webView.bounds.width, y: y * webView.bounds.height), to: window)
|
|
38
|
+
sample["x"] = point.x / window.bounds.width; sample["y"] = point.y / window.bounds.height
|
|
39
|
+
geometry["viewportWidth"] = window.bounds.width; geometry["viewportHeight"] = window.bounds.height
|
|
40
|
+
geometry["scale"] = window.screen.scale
|
|
41
|
+
payload["sample"] = sample; payload["geometry"] = geometry
|
|
42
|
+
let json = try JSONSerialization.data(withJSONObject: payload)
|
|
43
|
+
try AnsightPencil.captureFrameworkSample(String(decoding: json, as: UTF8.self)); call.resolve()
|
|
44
|
+
} catch { call.reject(error.localizedDescription, "pen_capture_failed", error) }
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
@objc public func hitTest(_ call: CAPPluginCall) {
|
|
49
|
+
DispatchQueue.main.async {
|
|
50
|
+
guard let webView = self.bridge?.webView, let window = webView.window, window.isKeyWindow,
|
|
51
|
+
let x = call.getDouble("x"), let y = call.getDouble("y"),
|
|
52
|
+
let width = call.getDouble("width"), width > 0,
|
|
53
|
+
let height = call.getDouble("height"), height > 0 else { call.resolve(["value": false]); return }
|
|
54
|
+
let point = webView.convert(CGPoint(x: x / width * webView.bounds.width, y: y / height * webView.bounds.height), to: window)
|
|
55
|
+
let hit = window.hitTest(point, with: nil)
|
|
56
|
+
call.resolve(["value": hit === webView || hit?.isDescendant(of: webView) == true])
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
@objc public func register(_ call: CAPPluginCall) {
|
|
61
|
+
do { call.resolve(["value": try AnsightPencil.register()]) }
|
|
62
|
+
catch { call.reject(error.localizedDescription, "pen_registration_failed", error) }
|
|
63
|
+
}
|
|
64
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,49 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ansight/capacitor-pencil",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.9.0",
|
|
4
|
+
"description": "Optional Ansight Pencil simulation for Capacitor on iOS Simulator.",
|
|
5
|
+
"main": "dist/cjs/index.js",
|
|
6
|
+
"types": "dist/esm/index.d.ts",
|
|
7
|
+
"license": "SEE LICENSE IN LICENSE",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/ansight-ai/ansight-sdk.git",
|
|
11
|
+
"directory": "src/capacitor-pencil"
|
|
12
|
+
},
|
|
13
|
+
"publishConfig": {
|
|
14
|
+
"access": "public"
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist",
|
|
18
|
+
"ios/Sources",
|
|
19
|
+
"Package.swift",
|
|
20
|
+
"AnsightCapacitorPencil.podspec",
|
|
21
|
+
"README.md",
|
|
22
|
+
"LICENSE"
|
|
23
|
+
],
|
|
24
|
+
"peerDependencies": {
|
|
25
|
+
"@ansight/capacitor": "1.9.0",
|
|
26
|
+
"@capacitor/core": ">=8.0.0 <9.0.0"
|
|
27
|
+
},
|
|
28
|
+
"dependencies": {
|
|
29
|
+
"@ansight/pencil-runtime": "1.9.0"
|
|
30
|
+
},
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"typescript": "~5.9.2",
|
|
33
|
+
"@capacitor/core": "^8.4.2",
|
|
34
|
+
"@ansight/capacitor": "file:../capacitor"
|
|
35
|
+
},
|
|
36
|
+
"scripts": {
|
|
37
|
+
"build": "npm run clean && tsc && tsc --module commonjs --outDir dist/cjs",
|
|
38
|
+
"test": "npm run build && node --test test/*.test.js",
|
|
39
|
+
"verify": "npm test",
|
|
40
|
+
"prepack": "npm run build",
|
|
41
|
+
"clean": "node -e \"require('node:fs').rmSync('dist', { recursive: true, force: true })\""
|
|
42
|
+
},
|
|
43
|
+
"capacitor": {
|
|
44
|
+
"ios": {
|
|
45
|
+
"src": "ios"
|
|
46
|
+
}
|
|
47
|
+
},
|
|
48
|
+
"module": "dist/esm/index.js"
|
|
49
|
+
}
|