@prosekit/lit 0.1.8 → 0.2.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.
@@ -1,6 +1,7 @@
1
1
  import {
2
- Popover
3
- } from "./chunk-CUH6G34Q.js";
2
+ Popover,
3
+ popoverPropsNames
4
+ } from "./chunk-OZUEYWYU.js";
4
5
  import {
5
6
  defineCustomElement
6
7
  } from "./chunk-S32IZIQF.js";
@@ -8,9 +9,66 @@ import {
8
9
  // src/components/inline-popover/index.ts
9
10
  import "@prosekit/core";
10
11
 
11
- // src/components/inline-popover/controller.ts
12
+ // src/controllers/use-editor-focus-event.ts
13
+ import {
14
+ defineFocusChangeHandler
15
+ } from "@prosekit/core";
16
+
17
+ // src/controllers/use-editor-extension.ts
18
+ import "@prosekit/core";
19
+ function useEditorExtension(host, extension) {
20
+ let cleanup;
21
+ let editor;
22
+ const update = () => {
23
+ cleanup == null ? void 0 : cleanup();
24
+ cleanup = editor == null ? void 0 : editor.use(extension);
25
+ };
26
+ const hostConnected = () => {
27
+ editor = host.editor;
28
+ update();
29
+ };
30
+ const hostUpdated = () => {
31
+ const newEditor = host.editor;
32
+ if (editor !== newEditor) {
33
+ editor = newEditor;
34
+ update();
35
+ }
36
+ };
37
+ const hostDisconnected = () => {
38
+ cleanup == null ? void 0 : cleanup();
39
+ cleanup = void 0;
40
+ };
41
+ host.addController({
42
+ hostConnected,
43
+ hostDisconnected,
44
+ hostUpdated
45
+ });
46
+ }
47
+
48
+ // src/controllers/use-editor-focus-event.ts
49
+ function useEditorFocusChangeEvent(host, handler) {
50
+ const extension = defineFocusChangeHandler(handler);
51
+ useEditorExtension(host, extension);
52
+ }
53
+
54
+ // src/controllers/use-editor-update-event.ts
12
55
  import { defineUpdateHandler } from "@prosekit/core";
13
- import "lit";
56
+ function useEditorUpdateEvent(host, handler) {
57
+ const extension = defineUpdateHandler(handler);
58
+ useEditorExtension(host, extension);
59
+ }
60
+
61
+ // src/controllers/use-pointer-down-event.ts
62
+ function usePointerDownEvent(host, handler) {
63
+ host.addController({
64
+ hostConnected: () => {
65
+ host.addEventListener("pointerdown", handler);
66
+ },
67
+ hostDisconnected: () => {
68
+ host.removeEventListener("pointerdown", handler);
69
+ }
70
+ });
71
+ }
14
72
 
15
73
  // src/components/inline-popover/helpers.ts
16
74
  import { isNodeSelection, isTextSelection } from "@prosekit/core";
@@ -28,7 +86,14 @@ function getVirtualSelectionElement(view) {
28
86
  }
29
87
  const selection = view.state.selection;
30
88
  if (!selection.empty && !isInCodeBlock(selection) && (isTextSelection(selection) || isNodeSelection(selection))) {
31
- return getDomRange();
89
+ const range = getDomRange();
90
+ if (!range)
91
+ return;
92
+ return {
93
+ contextElement: view.dom,
94
+ getBoundingClientRect: () => range.getBoundingClientRect(),
95
+ getClientRects: () => range.getClientRects()
96
+ };
32
97
  }
33
98
  }
34
99
  function getDomRange() {
@@ -43,113 +108,69 @@ function getDomRange() {
43
108
  return range;
44
109
  }
45
110
 
46
- // src/components/inline-popover/controller.ts
47
- var InlinePopoverController = class {
48
- constructor(host) {
49
- this.host = host;
50
- this.mouseHovering = false;
51
- this.host.addController(this);
52
- }
53
- setEditor(editor) {
54
- if (this.editor !== editor) {
55
- this.editor = editor;
56
- this.defineExtension();
57
- this.host.requestUpdate();
111
+ // src/components/inline-popover/use-inline-popover.ts
112
+ function useInlinePopover(host, onReferenceChange) {
113
+ let interacting = false;
114
+ let reference;
115
+ usePointerDownEvent(host, () => {
116
+ interacting = true;
117
+ });
118
+ useEditorFocusChangeEvent(host, (focus) => {
119
+ if (focus) {
120
+ interacting = false;
58
121
  }
59
- }
60
- hostConnected() {
61
- var _a;
62
- const handleMouseDown = () => {
63
- this.mouseHovering = true;
64
- };
65
- const handleMouseUp = () => {
66
- this.mouseHovering = false;
67
- this.update();
68
- };
69
- document.addEventListener("mousedown", handleMouseDown);
70
- document.addEventListener("mouseup", handleMouseUp);
71
- (_a = this.cleanupEventListener) == null ? void 0 : _a.call(this);
72
- this.cleanupEventListener = () => {
73
- document.removeEventListener("mousedown", handleMouseDown);
74
- document.removeEventListener("mouseup", handleMouseUp);
75
- };
76
- }
77
- hostDisconnected() {
78
- var _a, _b;
79
- (_a = this.cleanupExtension) == null ? void 0 : _a.call(this);
80
- this.cleanupExtension = void 0;
81
- (_b = this.cleanupEventListener) == null ? void 0 : _b.call(this);
82
- this.cleanupEventListener = void 0;
83
- }
84
- update() {
85
- const editor = this.editor;
86
- if (!editor || this.mouseHovering) {
122
+ });
123
+ useEditorUpdateEvent(host, (view) => {
124
+ if (interacting) {
87
125
  return;
88
126
  }
89
- const reference = getVirtualSelectionElement(editor.view);
90
- if (this.reference !== reference) {
91
- this.reference = reference;
92
- this.host.requestUpdate();
127
+ const ref = getVirtualSelectionElement(view);
128
+ if (reference !== ref) {
129
+ reference = ref;
130
+ onReferenceChange(reference);
131
+ host.requestUpdate();
93
132
  }
94
- }
95
- defineExtension() {
96
- var _a;
97
- const editor = this.editor;
98
- if (!editor) {
99
- return;
100
- }
101
- const extension = defineUpdateHandler(() => this.update());
102
- (_a = this.cleanupExtension) == null ? void 0 : _a.call(this);
103
- this.cleanupExtension = editor.use(extension);
104
- }
105
- };
106
-
107
- // src/components/inline-popover/default-popover-options.ts
108
- import { inline, offset, shift } from "@floating-ui/dom";
109
- var defaultPopoverOptions = {
110
- placement: "top",
111
- strategy: "absolute",
112
- middleware: [
113
- inline(),
114
- offset(8),
115
- shift({ mainAxis: true, crossAxis: true, padding: 8 })
116
- ]
117
- };
133
+ });
134
+ const getReference = () => reference;
135
+ return getReference;
136
+ }
118
137
 
119
138
  // src/components/inline-popover/index.ts
120
- var propNames = ["editor", "popoverOptions"];
139
+ var propNames = ["editor", ...popoverPropsNames];
121
140
  var InlinePopover = class extends Popover {
141
+ /**
142
+ * @hidden
143
+ */
122
144
  constructor() {
123
- super(...arguments);
124
- this.popoverOptions = defaultPopoverOptions;
125
- this.dismiss = "escape";
126
- /**
127
- * @hidden
128
- */
129
- this.controller = new InlinePopoverController(this);
145
+ super();
146
+ this.positioning = {
147
+ strategy: "fixed",
148
+ placement: "top",
149
+ offset: 12,
150
+ flip: false,
151
+ hide: true,
152
+ shift: true,
153
+ overlap: true,
154
+ fitViewport: false,
155
+ inline: true
156
+ };
157
+ useInlinePopover(this, (reference) => {
158
+ this.reference = reference;
159
+ });
130
160
  }
131
161
  /**
132
162
  * @hidden
133
163
  */
134
- willUpdate() {
135
- var _a, _b;
136
- if (this.editor) {
137
- this.controller.setEditor(this.editor);
138
- }
139
- this.active = !!((_a = this.controller) == null ? void 0 : _a.reference);
140
- this.reference = (_b = this.controller.reference) != null ? _b : void 0;
141
- this.options = this.popoverOptions;
164
+ updated(changedProperties) {
165
+ super.updated(changedProperties);
166
+ this.open = !!this.reference;
142
167
  }
143
168
  /**
144
169
  * @hidden
145
170
  */
146
171
  hide() {
147
- var _a;
148
172
  super.hide();
149
- if ((_a = this.controller) == null ? void 0 : _a.reference) {
150
- this.controller.reference = void 0;
151
- this.reference = void 0;
152
- }
173
+ this.reference = void 0;
153
174
  }
154
175
  };
155
176
  /**
@@ -157,8 +178,7 @@ var InlinePopover = class extends Popover {
157
178
  */
158
179
  InlinePopover.properties = {
159
180
  ...Popover.properties,
160
- editor: { attribute: false },
161
- popoverOptions: { attribute: false }
181
+ editor: { attribute: false }
162
182
  };
163
183
  defineCustomElement("prosekit-inline-popover", InlinePopover);
164
184
  export {
@@ -1,5 +1,6 @@
1
1
  export { AutoUpdateOptions } from './_tsup-dts-rollup';
2
- export { PopoverOptions_alias_2 as PopoverOptions } from './_tsup-dts-rollup';
2
+ export { PositioningOptions_alias_2 as PositioningOptions } from './_tsup-dts-rollup';
3
+ export { popoverPropsNames } from './_tsup-dts-rollup';
3
4
  export { propNames_alias_13 as propNames } from './_tsup-dts-rollup';
4
5
  export { PopoverProps } from './_tsup-dts-rollup';
5
6
  export { Popover } from './_tsup-dts-rollup';
@@ -1,9 +1,10 @@
1
1
  import {
2
2
  Popover,
3
- propNames
4
- } from "./chunk-CUH6G34Q.js";
3
+ popoverPropsNames
4
+ } from "./chunk-OZUEYWYU.js";
5
5
  import "./chunk-S32IZIQF.js";
6
6
  export {
7
7
  Popover,
8
- propNames
8
+ popoverPropsNames,
9
+ popoverPropsNames as propNames
9
10
  };
@@ -17,9 +17,14 @@ var propNames = [
17
17
  "onSizeChangeEnd"
18
18
  ];
19
19
  var Resizable = class extends LightElement {
20
+ /**
21
+ * @hidden
22
+ */
20
23
  constructor() {
21
- super(...arguments);
22
- this.context = new ContextProvider(this, {
24
+ super();
25
+ this.startWidth = 0;
26
+ this.startHeight = 0;
27
+ new ContextProvider(this, {
23
28
  context: resizableContext,
24
29
  initialValue: {
25
30
  onResizeStart: () => this.handleResizeStart(),
@@ -27,8 +32,6 @@ var Resizable = class extends LightElement {
27
32
  onResizeEnd: () => this.handleResizeEnd()
28
33
  }
29
34
  });
30
- this.startWidth = 0;
31
- this.startHeight = 0;
32
35
  }
33
36
  handleResizeStart() {
34
37
  var _a, _b;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@prosekit/lit",
3
3
  "type": "module",
4
- "version": "0.1.8",
4
+ "version": "0.2.0",
5
5
  "private": false,
6
6
  "author": {
7
7
  "name": "ocavue",
@@ -95,20 +95,23 @@
95
95
  "dist"
96
96
  ],
97
97
  "dependencies": {
98
- "@floating-ui/dom": "^1.5.4",
98
+ "@floating-ui/dom": "^1.5.3",
99
99
  "@lit/context": "^1.1.0",
100
- "@prosekit/core": "^0.2.2",
101
- "@prosekit/extensions": "^0.2.2",
100
+ "@prosekit/core": "^0.2.4",
101
+ "@prosekit/extensions": "^0.2.4",
102
102
  "@prosekit/pm": "^0.1.1",
103
103
  "@superhuman/command-score": "^0.5.0",
104
- "lit": "^3.1.0"
104
+ "@zag-js/dismissable": "^0.32.0",
105
+ "@zag-js/dom-query": "^0.32.0",
106
+ "@zag-js/utils": "^0.32.0",
107
+ "lit": "^3.1.1"
105
108
  },
106
109
  "devDependencies": {
107
110
  "@prosekit/dev": "*",
108
111
  "minify-literals": "^1.0.8",
109
112
  "tsup": "^8.0.1",
110
113
  "typescript": "^5.3.3",
111
- "vitest": "^1.1.3"
114
+ "vitest": "^1.2.0"
112
115
  },
113
116
  "scripts": {
114
117
  "build:tsup": "tsup",
@@ -1,224 +0,0 @@
1
- import {
2
- LightElement,
3
- defineCustomElement
4
- } from "./chunk-S32IZIQF.js";
5
-
6
- // src/components/popover/index.ts
7
- import {
8
- autoUpdate,
9
- computePosition
10
- } from "@floating-ui/dom";
11
-
12
- // src/utils/popover-api.ts
13
- var popoverAvailable = typeof HTMLElement !== "undefined" && HTMLElement.prototype.hasOwnProperty("popover");
14
- var boundary = popoverAvailable && typeof document !== "undefined" && document.body || void 0;
15
-
16
- // src/utils/round-by-dpr.ts
17
- function roundByDPR(value) {
18
- const dpr = window.devicePixelRatio || 1;
19
- return Math.round(value * dpr) / dpr;
20
- }
21
-
22
- // src/components/popover/default-popover-options.ts
23
- import {
24
- offset,
25
- shift,
26
- size
27
- } from "@floating-ui/dom";
28
-
29
- // src/components/popover/options.ts
30
- import "@floating-ui/dom";
31
-
32
- // src/components/popover/default-popover-options.ts
33
- var defaultDetectOverflowOptions = {
34
- padding: 8,
35
- boundary
36
- };
37
- var defaultPopoverOptions = {
38
- placement: "bottom",
39
- middleware: [
40
- offset({ mainAxis: 2, crossAxis: 0 }),
41
- shift({ ...defaultDetectOverflowOptions }),
42
- size({
43
- apply: ({ availableWidth, availableHeight, elements }) => {
44
- elements.floating.style.setProperty(
45
- "max-width",
46
- `${Math.floor(availableWidth)}px`
47
- );
48
- elements.floating.style.setProperty(
49
- "max-height",
50
- `${Math.floor(availableHeight)}px`
51
- );
52
- },
53
- ...defaultDetectOverflowOptions
54
- })
55
- ]
56
- };
57
-
58
- // src/components/popover/index.ts
59
- var propNames = [
60
- "active",
61
- "reference",
62
- "options",
63
- "autoUpdate",
64
- "autoUpdateOptions"
65
- ];
66
- var Popover = class extends LightElement {
67
- constructor() {
68
- super(...arguments);
69
- /**
70
- * Controls the visibility of the popover element. When set to `true`, the
71
- * popover is displayed and positioned relative to its reference element. When
72
- * set to `false`, the popover is hidden and its positioning logic is
73
- * deactivated.
74
- */
75
- this.active = false;
76
- /**
77
- * Controls whether the popover position is automatically updated when the
78
- * reference element changes position. When set to `true`, the popover
79
- * position is updated automatically. When set to `false`, the popover
80
- * position is only updated when the given properties are changed.
81
- *
82
- * @default false
83
- */
84
- this.autoUpdate = false;
85
- /**
86
- * Controls whether the popover should be dismissed based on user interaction.
87
- *
88
- * Available options:
89
- *
90
- * - "off": The popover is not dismissed.
91
- * - "on": The popover is dismissed when the user clicks outside of the popover or presses the escape key.
92
- * - "click": The popover is dismissed when the user clicks outside of the popover.
93
- * - "escape": The popover is dismissed when the user presses the escape key.
94
- *
95
- * @default "on"
96
- */
97
- this.dismiss = "on";
98
- this.handleDocumentMouseDown = (event) => {
99
- const path = event.composedPath();
100
- if (!path.includes(this)) {
101
- this.hide();
102
- }
103
- };
104
- this.handleDocumentKeyDown = (event) => {
105
- if (event.key === "Escape" && this.active) {
106
- event.stopPropagation();
107
- this.hide();
108
- return;
109
- }
110
- };
111
- }
112
- /**
113
- * @hidden
114
- */
115
- connectedCallback() {
116
- super.connectedCallback();
117
- const clickEnabled = this.dismiss === "on" || this.dismiss === "click";
118
- const escapeEnabled = this.dismiss === "on" || this.dismiss === "escape";
119
- const handleMouseDown = clickEnabled ? this.handleDocumentMouseDown.bind(this) : null;
120
- const handleKeyDown = escapeEnabled ? this.handleDocumentKeyDown.bind(this) : null;
121
- handleMouseDown && document.addEventListener("mousedown", handleMouseDown);
122
- handleKeyDown && document.addEventListener("keydown", handleKeyDown);
123
- this.disposeEventListeners = () => {
124
- handleMouseDown && document.removeEventListener("mousedown", handleMouseDown);
125
- handleKeyDown && document.removeEventListener("keydown", handleKeyDown);
126
- };
127
- }
128
- /**
129
- * @hidden
130
- */
131
- disconnectedCallback() {
132
- var _a, _b;
133
- super.disconnectedCallback();
134
- (_a = this.disposeAutoUpdate) == null ? void 0 : _a.call(this);
135
- this.disposeAutoUpdate = void 0;
136
- (_b = this.disposeEventListeners) == null ? void 0 : _b.call(this);
137
- this.disposeEventListeners = void 0;
138
- }
139
- /**
140
- * @hidden
141
- */
142
- updated(changedProperties) {
143
- var _a;
144
- super.updated(changedProperties);
145
- if (popoverAvailable && this.isConnected) {
146
- this.popover = "manual";
147
- (_a = this.togglePopover) == null ? void 0 : _a.call(this, this.active);
148
- }
149
- this.start();
150
- this.setHidden(!this.active);
151
- }
152
- /**
153
- * @hidden
154
- */
155
- start() {
156
- var _a;
157
- (_a = this.disposeAutoUpdate) == null ? void 0 : _a.call(this);
158
- this.disposeAutoUpdate = void 0;
159
- const reference = this.reference;
160
- if (!reference)
161
- return;
162
- if (!this.active)
163
- return;
164
- if (this.autoUpdate) {
165
- this.disposeAutoUpdate = autoUpdate(
166
- reference,
167
- this,
168
- () => void this.compute(),
169
- this.autoUpdateOptions
170
- );
171
- } else {
172
- void this.compute();
173
- }
174
- }
175
- /**
176
- * @hidden
177
- */
178
- async compute() {
179
- var _a, _b, _c;
180
- const reference = this.reference;
181
- if (!reference)
182
- return;
183
- if (!this.active)
184
- return;
185
- this.setHidden(false);
186
- this.style.setProperty("margin", "0");
187
- this.style.setProperty("top", "0");
188
- this.style.setProperty("left", "0");
189
- this.style.setProperty("position", (_b = (_a = this.options) == null ? void 0 : _a.strategy) != null ? _b : "absolute");
190
- const options = (_c = this.options) != null ? _c : defaultPopoverOptions;
191
- const computed = await computePosition(reference, this, options);
192
- const { x, y, strategy } = computed != null ? computed : { x: 0, y: 0, strategy: "absolute" };
193
- this.style.setProperty("opacity", "1");
194
- this.style.setProperty("position", strategy);
195
- this.style.setProperty(
196
- "transform",
197
- `translate(${roundByDPR(x)}px,${roundByDPR(y)}px)`
198
- );
199
- }
200
- /**
201
- * @hidden
202
- */
203
- hide() {
204
- this.active = false;
205
- }
206
- };
207
- /**
208
- * @hidden
209
- */
210
- Popover.properties = {
211
- active: { type: Boolean, reflect: true },
212
- reference: { attribute: false },
213
- options: { attribute: false },
214
- autoUpdate: { type: Boolean, reflect: true },
215
- autoUpdateOptions: { type: Object },
216
- dismiss: { type: String, reflect: true }
217
- };
218
- defineCustomElement("prosekit-popover", Popover);
219
-
220
- export {
221
- boundary,
222
- propNames,
223
- Popover
224
- };