@prosekit/lit 0.1.9 → 0.2.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.
@@ -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,54 @@ 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 { trackDismissableElement } from "@zag-js/dismissable";
56
+ function useEditorUpdateEvent(host, handler) {
57
+ const extension = defineUpdateHandler(handler);
58
+ useEditorExtension(host, extension);
59
+ }
14
60
 
15
61
  // src/components/inline-popover/helpers.ts
16
62
  import { isNodeSelection, isTextSelection } from "@prosekit/core";
@@ -28,7 +74,14 @@ function getVirtualSelectionElement(view) {
28
74
  }
29
75
  const selection = view.state.selection;
30
76
  if (!selection.empty && !isInCodeBlock(selection) && (isTextSelection(selection) || isNodeSelection(selection))) {
31
- return getDomRange();
77
+ const range = getDomRange();
78
+ if (!range)
79
+ return;
80
+ return {
81
+ contextElement: view.dom,
82
+ getBoundingClientRect: () => range.getBoundingClientRect(),
83
+ getClientRects: () => range.getClientRects()
84
+ };
32
85
  }
33
86
  }
34
87
  function getDomRange() {
@@ -43,115 +96,66 @@ function getDomRange() {
43
96
  return range;
44
97
  }
45
98
 
46
- // src/components/inline-popover/controller.ts
47
- var InlinePopoverController = class {
48
- constructor(host) {
49
- this.host = host;
50
- this.interacting = 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();
58
- }
59
- }
60
- hostConnected() {
61
- var _a;
62
- const handlePointerDown = () => {
63
- this.interacting = true;
64
- };
65
- this.host.addEventListener("pointerdown", handlePointerDown);
66
- const cleanupDismissable = trackDismissableElement(this.host, {
67
- defer: true,
68
- pointerBlocking: false,
69
- onDismiss: () => {
70
- this.interacting = false;
71
- }
72
- });
73
- (_a = this.cleanupEventListener) == null ? void 0 : _a.call(this);
74
- this.cleanupEventListener = () => {
75
- this.host.removeEventListener("pointerdown", handlePointerDown);
76
- cleanupDismissable();
77
- };
78
- }
79
- hostDisconnected() {
80
- var _a, _b;
81
- (_a = this.cleanupExtension) == null ? void 0 : _a.call(this);
82
- this.cleanupExtension = void 0;
83
- (_b = this.cleanupEventListener) == null ? void 0 : _b.call(this);
84
- this.cleanupEventListener = void 0;
85
- }
86
- update() {
87
- const editor = this.editor;
88
- if (!editor || this.interacting) {
99
+ // src/components/inline-popover/use-inline-popover.ts
100
+ function useInlinePopover(host, onReferenceChange) {
101
+ let reference;
102
+ let editorFocused = false;
103
+ const isPopoverFocused = () => {
104
+ return !editorFocused && host.contains(host.ownerDocument.activeElement);
105
+ };
106
+ useEditorFocusChangeEvent(host, (focus) => {
107
+ editorFocused = focus;
108
+ });
109
+ useEditorUpdateEvent(host, (view) => {
110
+ if (isPopoverFocused())
89
111
  return;
112
+ const ref = getVirtualSelectionElement(view);
113
+ if (reference !== ref) {
114
+ reference = ref;
115
+ onReferenceChange(reference);
116
+ host.requestUpdate();
90
117
  }
91
- const reference = getVirtualSelectionElement(editor.view);
92
- if (this.reference !== reference) {
93
- this.reference = reference;
94
- this.host.requestUpdate();
95
- }
96
- }
97
- defineExtension() {
98
- var _a;
99
- const editor = this.editor;
100
- if (!editor) {
101
- return;
102
- }
103
- const extension = defineUpdateHandler(() => this.update());
104
- (_a = this.cleanupExtension) == null ? void 0 : _a.call(this);
105
- this.cleanupExtension = editor.use(extension);
106
- }
107
- };
108
-
109
- // src/components/inline-popover/default-popover-options.ts
110
- import { inline, offset, shift } from "@floating-ui/dom";
111
- var defaultPopoverOptions = {
112
- placement: "top",
113
- strategy: "absolute",
114
- middleware: [
115
- inline(),
116
- offset(8),
117
- shift({ mainAxis: true, crossAxis: true, padding: 8 })
118
- ]
119
- };
118
+ });
119
+ const getReference = () => reference;
120
+ return getReference;
121
+ }
120
122
 
121
123
  // src/components/inline-popover/index.ts
122
- var propNames = ["editor", "popoverOptions"];
124
+ var propNames = ["editor", ...popoverPropsNames];
123
125
  var InlinePopover = class extends Popover {
126
+ /**
127
+ * @hidden
128
+ */
124
129
  constructor() {
125
- super(...arguments);
126
- this.popoverOptions = defaultPopoverOptions;
127
- this.dismiss = "escape";
128
- /**
129
- * @hidden
130
- */
131
- this.controller = new InlinePopoverController(this);
130
+ super();
131
+ this.positioning = {
132
+ strategy: "fixed",
133
+ placement: "top",
134
+ offset: 12,
135
+ flip: false,
136
+ hide: true,
137
+ shift: true,
138
+ overlap: true,
139
+ fitViewport: false,
140
+ inline: true
141
+ };
142
+ useInlinePopover(this, (reference) => {
143
+ this.reference = reference;
144
+ });
132
145
  }
133
146
  /**
134
147
  * @hidden
135
148
  */
136
- willUpdate() {
137
- var _a, _b;
138
- if (this.editor) {
139
- this.controller.setEditor(this.editor);
140
- }
141
- this.active = !!((_a = this.controller) == null ? void 0 : _a.reference);
142
- this.reference = (_b = this.controller.reference) != null ? _b : void 0;
143
- this.options = this.popoverOptions;
149
+ updated(changedProperties) {
150
+ super.updated(changedProperties);
151
+ this.open = !!this.reference;
144
152
  }
145
153
  /**
146
154
  * @hidden
147
155
  */
148
156
  hide() {
149
- var _a;
150
157
  super.hide();
151
- if ((_a = this.controller) == null ? void 0 : _a.reference) {
152
- this.controller.reference = void 0;
153
- this.reference = void 0;
154
- }
158
+ this.reference = void 0;
155
159
  }
156
160
  };
157
161
  /**
@@ -159,8 +163,7 @@ var InlinePopover = class extends Popover {
159
163
  */
160
164
  InlinePopover.properties = {
161
165
  ...Popover.properties,
162
- editor: { attribute: false },
163
- popoverOptions: { attribute: false }
166
+ editor: { attribute: false }
164
167
  };
165
168
  defineCustomElement("prosekit-inline-popover", InlinePopover);
166
169
  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
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@prosekit/lit",
3
3
  "type": "module",
4
- "version": "0.1.9",
4
+ "version": "0.2.1",
5
5
  "private": false,
6
6
  "author": {
7
7
  "name": "ocavue",
@@ -97,11 +97,13 @@
97
97
  "dependencies": {
98
98
  "@floating-ui/dom": "^1.5.4",
99
99
  "@lit/context": "^1.1.0",
100
- "@prosekit/core": "^0.2.4",
101
- "@prosekit/extensions": "^0.2.3",
100
+ "@prosekit/core": "^0.2.6",
101
+ "@prosekit/extensions": "^0.2.5",
102
102
  "@prosekit/pm": "^0.1.1",
103
103
  "@superhuman/command-score": "^0.5.0",
104
- "@zag-js/dismissable": "^0.32.0",
104
+ "@zag-js/dismissable": "^0.32.1",
105
+ "@zag-js/dom-query": "^0.32.1",
106
+ "@zag-js/utils": "^0.32.1",
105
107
  "lit": "^3.1.1"
106
108
  },
107
109
  "devDependencies": {
@@ -109,7 +111,7 @@
109
111
  "minify-literals": "^1.0.8",
110
112
  "tsup": "^8.0.1",
111
113
  "typescript": "^5.3.3",
112
- "vitest": "^1.1.3"
114
+ "vitest": "^1.2.1"
113
115
  },
114
116
  "scripts": {
115
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
- };
package/src/index.ts DELETED
@@ -1 +0,0 @@
1
- export {}