js.foresight-devtools 1.2.1 → 1.3.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/README.md CHANGED
@@ -1,25 +1,17 @@
1
- # ForesightJS Development Tools
1
+ # js.foresight-devtools
2
2
 
3
3
  [![npm version](https://img.shields.io/npm/v/js.foresight-devtools.svg)](https://www.npmjs.com/package/js.foresight-devtools)
4
4
  [![TypeScript](https://img.shields.io/badge/%3C%2F%3E-TypeScript-%230074c1.svg)](http://www.typescriptlang.org/)
5
5
  [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
6
6
 
7
- Visual development tools for [ForesightJS](https://foresightjs.com/) - a library that predicts user intent by analyzing mouse movements, scroll behavior, and keyboard navigation to enable proactive actions like prefetching.
7
+ `ForesightJS` offers dedicated [Development Tools](https://github.com/spaansba/ForesightJS/tree/main/packages/js.foresight-devtools), written in [Lit](https://lit.dev/), to help you better understand and fine-tune how `ForesightJS` works within your application. You can see the development tools in action on the [playground page](https://foresightjs.com/#playground), which includes visual trajectory indicators, element boundaries, and a control panel in the bottom-right corner.
8
8
 
9
- ## What are the ForesightJS Development Tools?
10
-
11
- The ForesightJS Development Tools are a companion development package that provides visual development capabilities for ForesightJS implementations. They help developers understand and tune how ForesightJS is working in their applications by providing real-time visualization of:
12
-
13
- - **Mouse trajectory predictions** - See predicted cursor paths and intersection points
14
- - **Element bounds and hit slop areas** - Visualize registered elements and their interaction zones
15
- - **Keyboard navigation sequences** - Track tab-based navigation predictions
16
- - **Callback execution** - Monitor when and why prediction callbacks fire
17
- - **Real-time settings control** - Adjust ForesightJS parameters on the fly
18
-
19
- ![ForesightJS Development Tools Demo](https://github.com/user-attachments/assets/36c81a82-fee7-43d6-ba1e-c48214136f90)
9
+ These tools are built entirely using `ForesightJS`'s [built-in events](/docs/events), demonstrating how you can create your own monitoring and debugging tools using the same event system.
20
10
 
21
11
  ## Installation
22
12
 
13
+ To install the `ForesightJS` Development Tools package, use your preferred package manager:
14
+
23
15
  ```bash
24
16
  pnpm add -D js.foresight-devtools
25
17
  # or
@@ -28,142 +20,71 @@ npm install -D js.foresight-devtools
28
20
  yarn add -D js.foresight-devtools
29
21
  ```
30
22
 
31
- ## Basic Usage
32
-
33
- ```javascript
34
- import { ForesightManager } from "js.foresight"
35
- import { ForesightDevtools } from "js.foresight-devtools"
36
-
37
- // Initialize ForesightJS
38
- ForesightManager.initialize()
39
-
40
- // Initialize development tools
41
- ForesightDevtools.initialize()
42
- ```
43
-
44
- ## Configuration Options
45
-
46
- ```typescript
47
- type DevelopmentToolsSettings = {
48
- showDebugger?: boolean
49
- isControlPanelDefaultMinimized?: boolean
50
- showNameTags?: boolean // Show element names on overlays
51
- sortElementList?: "documentOrder" | "visibility" | "insertionOrder" // Control panel sorting
52
- logging: {
53
- logLocation: "controlPanel" | "console" | "both" | "none" // Where to log the Foresight Events
54
- callbackCompleted: boolean
55
- callbackInvoked: boolean
56
- elementDataUpdated: boolean
57
- elementRegistered: boolean
58
- elementUnregistered: boolean
59
- managerSettingsChanged: boolean
60
- mouseTrajectoryUpdate: boolean
61
- scrollTrajectoryUpdate: boolean
62
- }
63
- }
64
- ```
65
-
66
- ### Available Development Tools Settings
67
-
68
- **TypeScript Type:** `DevelopmentToolsSettings`
69
-
70
- | Setting | Type | Default | Description |
71
- | -------------------------------- | ----------------- | ------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
72
- | `showDebugger` | `boolean` | `true` | Controls whether the development tools are visible and active |
73
- | `isControlPanelDefaultMinimized` | `boolean` | `false` | When true, the development tools control panel will be minimized on page load |
74
- | `showNameTags` | `boolean` | `true` | Shows the element `name` (or `id` if no `name` is given) above registered elements |
75
- | `sortElementList` | `SortElementList` | `visibility` | Controls element sorting in control panel: `visibility` sorts by viewport visibility, `documentOrder` sorts by HTML structure order, `insertionOrder` sorts by registration order |
76
-
77
- ### Logging Configuration
78
-
79
- **TypeScript Type:** `LogEvents & { logLocation: LoggingLocations }`
80
-
81
- | Setting | Type | Default | Description |
82
- | ------------------------ | ------------------------------------------------------- | ---------------- | ---------------------------------------------------------------------------------------------- |
83
- | `logLocation` | `"controlPanel"` \| `"console"` \| `"both"` \| `"none"` | `"controlPanel"` | Where to output the ForesightJS event logs |
84
- | `callbackCompleted` | `boolean` | `false` | Log when element callbacks finish executing (includes success/error status and execution time) |
85
- | `callbackInvoked` | `boolean` | `false` | Log when element callbacks are triggered (includes hit type: mouse/keyboard/scroll) |
86
- | `elementDataUpdated` | `boolean` | `false` | Log when element data changes (bounds updates, visibility changes) |
87
- | `elementRegistered` | `boolean` | `false` | Log when new elements are registered with ForesightJS |
88
- | `elementUnregistered` | `boolean` | `false` | Log when elements are unregistered (includes reason: callbackHit/disconnected/apiCall) |
89
- | `managerSettingsChanged` | `boolean` | `false` | Log when ForesightManager global settings are modified |
90
- | `mouseTrajectoryUpdate` | `boolean` | `false` | Log real-time mouse trajectory predictions (high frequency - use with caution) |
91
- | `scrollTrajectoryUpdate` | `boolean` | `false` | Log scroll direction predictions and trajectory updates |
92
-
93
- ### Usage Example with All Options
23
+ ## Enabling Development Tools
94
24
 
95
25
  ```javascript
96
26
  import { ForesightManager } from "js.foresight"
97
27
  import { ForesightDevtools } from "js.foresight-devtools"
98
28
 
99
29
  // Initialize ForesightJS
100
- ForesightManager.initialize({
101
- trajectoryPredictionTime: 100,
102
- defaultHitSlop: { top: 10, right: 10, bottom: 10, left: 10 },
103
- enableMousePrediction: true,
104
- enableTabPrediction: true,
105
- enableScrollPrediction: true,
106
- })
30
+ ForesightManager.initialize({})
107
31
 
108
- // Initialize development tools with custom settings
32
+ // Initialize the development tools (all options are optional)
109
33
  ForesightDevtools.initialize({
110
34
  showDebugger: true,
111
- isControlPanelDefaultMinimized: false,
112
- showNameTags: true,
113
- sortElementList: "visibility",
35
+ isControlPanelDefaultMinimized: false, // optional setting which allows you to minimize the control panel on default
36
+ showNameTags: true, // optional setting which shows the name of the element
37
+ sortElementList: "visibility", // optional setting for how the elements in the control panel are sorted
114
38
  logging: {
115
- logLocation: "controlPanel",
39
+ logLocation: "controlPanel", // Where to log the Foresight Events
116
40
  callbackCompleted: true,
41
+ elementReactivated: true,
117
42
  callbackInvoked: true,
118
- elementRegistered: true,
119
- elementUnregistered: true,
120
- elementDataUpdated: false, // High frequency - keep disabled for performance
43
+ elementDataUpdated: false,
44
+ elementRegistered: false,
45
+ elementUnregistered: false,
121
46
  managerSettingsChanged: true,
122
- mouseTrajectoryUpdate: false, // High frequency - keep disabled for performance
123
- scrollTrajectoryUpdate: false, // High frequency - keep disabled for performance
124
- },
125
- })
126
-
127
- // Register elements as usual
128
- ForesightManager.instance.register({
129
- element: document.getElementById("my-button"),
130
- callback: () => {
131
- console.log("Prediction triggered!")
47
+ mouseTrajectoryUpdate: false, // dont log this to the devtools
48
+ scrollTrajectoryUpdate: false, // dont log this to the devtools
49
+ deviceStrategyChanged: true,
132
50
  },
133
- name: "my-button", // This name will appear in the development tools
134
51
  })
135
52
  ```
136
53
 
137
- ## Development Workflow
54
+ ## Development Tools Features
55
+
56
+ Once enabled, the `ForesightJS` Development Tools add several visual layers to your application, including mouse and scroll trajectories and element hitboxes. A control panel also appears in the bottom-right corner of the screen.
57
+
58
+ ### Control Panel
59
+
60
+ The control panel provides three main tabs for debugging and configuration. Each tab serves a specific purpose in understanding and tuning ForesightJS behavior.
138
61
 
139
- The development tools are particularly useful when:
62
+ #### Settings Tab
140
63
 
141
- 1. **Setting up ForesightJS** for the first time in an application
142
- 2. **Fine-tuning prediction parameters** for specific UI components
143
- 3. **Debugging callback execution** issues or unexpected behavior
144
- 4. **Optimizing hit slop areas** for better user experience
145
- 5. **Understanding prediction accuracy** across different interaction patterns
64
+ The Settings tab provides real-time controls for all [Global Configurations](/docs/configuration/global-settings). Changes made through these controls immediately affect the `ForesightManager` configuration, allowing you to see how different settings impact your app without fiddling in your code.
146
65
 
147
- ## Framework Integration
66
+ #### Elements Tab
148
67
 
149
- Since both ForesightJS and the development tools are framework-agnostic, you can use them together in any JavaScript environment:
68
+ The Elements tab displays a overview of all currently registered elements within the `ForesightManage`r. Each element entry shows its current status through color-coded indicators:
150
69
 
151
- - React / Next.js
152
- - Vue / Nuxt
153
- - Angular
154
- - Svelte / SvelteKit
155
- - Vanilla JavaScript
70
+ - 🟢 **Green** - Active visible elements in desktop mode
71
+ - **Grey** - Active invisible elements in desktop mode
72
+ - 🟣 **Purple** - Active elements while in touch device mode (all elements, we dont track visibility in this mode)
73
+ - 🟡 **Yellow** - Elements which callbacks are currently executing
74
+ - 🔘 **Light Gray** - Inactive elements
156
75
 
157
- ## Requirements
76
+ Each element can also be expanded to reveal its [`ForesightElementData`](/docs/next/getting-started/typescript#foresightelementdata) information including settings, callback status, and metadata. A countdown timer appears for elements in their reactivation cooldown period (`reactivateAfter`), clicking this timer will instantly reactivate the element.
158
77
 
159
- - **js.foresight** ^3.0.0 (peer dependency)
78
+ #### Log Tab
160
79
 
161
- ## License
80
+ The Log tab displays real-time [events](/docs/events) emitted by `ForesightJS`. You can see callback execution times, the full element's lifecycle and other system events. Events can be filtered through the devtools initialization configuration or in the control panel itself.
162
81
 
163
- MIT © [Bart Spaans](https://github.com/spaansba)
82
+ You can also print out the complete [`ForesightManager.instance.getManagerData`](/docs/debugging/static-properties#foresightmanagerinstancegetmanagerdata) state without having to call it from your code.
164
83
 
165
- ## Related
84
+ :::caution
85
+ Avoid logging frequently emitted events to the browser console, as it can noticeably slow down your development environment. Use the control panel for this instead.
86
+ :::
166
87
 
167
- - [ForesightJS](https://foresightjs.com/) - The main prediction library
168
- - [Documentation](https://foresightjs.com/docs) - Comprehensive guides and API reference
169
- - [GitHub](https://github.com/spaansba/ForesightJS) - Source code and issues
88
+ :::note
89
+ Element overlay visualization and visibility sorting in the control panel only work with desktop/mouse prediction strategies. When debugging `touchDeviceStrategy` configurations, these features are not available as touch strategies don't track the same positioning data.
90
+ :::
package/dist/index.d.ts CHANGED
@@ -196,6 +196,10 @@ declare class SingleElement extends LitElement {
196
196
  isActive: boolean;
197
197
  isExpanded: boolean;
198
198
  onToggle: ((elementId: string) => void) | undefined;
199
+ private currentDeviceStrategy;
200
+ private _abortController;
201
+ connectedCallback(): void;
202
+ disconnectedCallback(): void;
199
203
  private getBorderColor;
200
204
  private getStatusIndicatorClass;
201
205
  private getStatusText;
@@ -273,6 +277,11 @@ interface ElementRegisteredPayload extends PayloadBase {
273
277
  }
274
278
  interface ElementUnregisteredEvent extends PayloadBase {
275
279
  type: "elementUnregistered";
280
+ name: string;
281
+ id: string;
282
+ callbackInfo: ElementCallbackInfo;
283
+ meta: Record<string, unknown>;
284
+ wasLastRegisteredElement: boolean;
276
285
  }
277
286
  interface ElementReactivatedPayload extends PayloadBase {
278
287
  type: "elementReactivated";
@@ -298,11 +307,13 @@ interface CallbackInvokedPayload extends PayloadBase {
298
307
  }
299
308
  interface CallbackCompletedPayload extends PayloadBase {
300
309
  type: "callbackCompleted";
310
+ elapsed: string;
301
311
  name: string;
302
312
  hitType: CallbackHitType;
303
313
  status: "success" | "error" | undefined;
304
314
  errorMessage: string | undefined | null;
305
315
  callbackInfo: ElementCallbackInfo;
316
+ wasLastActiveElement: boolean;
306
317
  meta: Record<string, unknown>;
307
318
  }
308
319
  interface MouseTrajectoryUpdatePayload extends PayloadBase {
@@ -323,6 +334,11 @@ interface ManagerSettingsChangedPayload extends PayloadBase {
323
334
  globalSettings: ForesightManagerSettings;
324
335
  settingsChanged: UpdatedManagerSetting[];
325
336
  }
337
+ interface DeviceStrategyChangedPayload extends PayloadBase {
338
+ type: "deviceStrategyChanged";
339
+ oldStrategy: string;
340
+ newStrategy: string;
341
+ }
326
342
  interface SerializationErrorPayload extends PayloadBase {
327
343
  type: "serializationError";
328
344
  error: "Failed to serialize event data";
@@ -338,7 +354,7 @@ interface ManagerDataPayload extends PayloadBase {
338
354
  elementInfo: string;
339
355
  }>;
340
356
  }
341
- type SerializedEventData = ElementRegisteredPayload | ElementUnregisteredEvent | ElementReactivatedPayload | ElementDataUpdatedPayload | CallbackInvokedPayload | CallbackCompletedPayload | MouseTrajectoryUpdatePayload | ScrollTrajectoryUpdatePayload | ManagerSettingsChangedPayload | ManagerDataPayload | SerializationErrorPayload;
357
+ type SerializedEventData = ElementRegisteredPayload | ElementUnregisteredEvent | ElementReactivatedPayload | ElementDataUpdatedPayload | CallbackInvokedPayload | CallbackCompletedPayload | MouseTrajectoryUpdatePayload | ScrollTrajectoryUpdatePayload | ManagerSettingsChangedPayload | DeviceStrategyChangedPayload | ManagerDataPayload | SerializationErrorPayload;
342
358
 
343
359
  declare class SingleLog extends LitElement {
344
360
  static styles: lit.CSSResult[];
@@ -454,6 +470,7 @@ declare class SettingsTab extends LitElement {
454
470
  private initialSettings;
455
471
  private devtoolsSettings;
456
472
  private changedSettings;
473
+ private touchDeviceStrategyOptions;
457
474
  private _abortController;
458
475
  constructor();
459
476
  connectedCallback(): void;
@@ -462,6 +479,7 @@ declare class SettingsTab extends LitElement {
462
479
  private _checkManagerSettingsChanges;
463
480
  private _checkDevtoolsSettingsChanges;
464
481
  private _handleDevtoolsSettingChange;
482
+ private _handleTouchDeviceStrategyChange;
465
483
  private handleCopySettings;
466
484
  private generateSettingsCode;
467
485
  render(): lit.TemplateResult<1>;
@@ -498,6 +516,11 @@ declare global {
498
516
 
499
517
  declare class DebugOverlay extends LitElement {
500
518
  static styles: lit.CSSResult[];
519
+ private _abortController;
520
+ private _strategy;
521
+ connectedCallback(): void;
522
+ private handleDeviceStrategyChange;
523
+ disconnectedCallback(): void;
501
524
  render(): lit.TemplateResult<1>;
502
525
  }
503
526
  declare global {