@prosekit/lit 0.0.2 → 0.0.4

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.
@@ -18,7 +18,7 @@ import { css } from "lit";
18
18
  var componentStyles = css`:host{box-sizing:border-box}:host *,:host ::after,:host ::before{box-sizing:inherit}`;
19
19
 
20
20
  // src/styles/block-component.styles.ts
21
- var blockComponentStyles = css2`${componentStyles}:host{display:block}`;
21
+ var blockComponentStyles = css2`${componentStyles}:host{display:block}:host([hidden]){display:none}`;
22
22
 
23
23
  export {
24
24
  __decorateClass,
@@ -0,0 +1,9 @@
1
+ // src/internal/command/list-context.ts
2
+ import { createContext } from "@lit-labs/context";
3
+ var commandListContext = createContext(
4
+ "prosekit-command-list-context"
5
+ );
6
+
7
+ export {
8
+ commandListContext
9
+ };
@@ -0,0 +1,21 @@
1
+ // src/internal/command/popover-context.ts
2
+ import { createContext } from "@lit-labs/context";
3
+ var commandPopoverContext = createContext(
4
+ "prosekit-command-popover-context"
5
+ );
6
+
7
+ // src/internal/command/utils.ts
8
+ function isCommandItem(element) {
9
+ var _a;
10
+ return ((_a = element == null ? void 0 : element.tagName) == null ? void 0 : _a.toLowerCase()) === "prosekit-command-item";
11
+ }
12
+ function isCommandList(element) {
13
+ var _a;
14
+ return ((_a = element == null ? void 0 : element.tagName) == null ? void 0 : _a.toLowerCase()) === "prosekit-command-list";
15
+ }
16
+
17
+ export {
18
+ commandPopoverContext,
19
+ isCommandItem,
20
+ isCommandList
21
+ };
@@ -0,0 +1,7 @@
1
+ type CommandListContext = {
2
+ scores: Map<string, number>;
3
+ selectedValue: string;
4
+ registerValue: (value: string) => VoidFunction;
5
+ };
6
+
7
+ export { CommandListContext as C };
@@ -0,0 +1,8 @@
1
+ type CommandPopoverContext = {
2
+ active: boolean;
3
+ query: string;
4
+ handleDismiss: VoidFunction;
5
+ handleSubmit: VoidFunction;
6
+ };
7
+
8
+ export { CommandPopoverContext as C };
@@ -0,0 +1,16 @@
1
+ import * as lit from 'lit';
2
+ import { LitElement, CSSResultGroup, PropertyValues } from 'lit';
3
+ import { C as CommandListContext } from './list-context-2692a600.js';
4
+
5
+ interface CommandEmptyProps {
6
+ }
7
+ declare class CommandEmpty extends LitElement implements CommandEmptyProps {
8
+ /** @hidden */
9
+ static styles: CSSResultGroup;
10
+ listContext?: CommandListContext;
11
+ protected willUpdate(_changedProperties: PropertyValues<CommandEmpty>): void;
12
+ /** @hidden */
13
+ render(): lit.TemplateResult<1> | null;
14
+ }
15
+
16
+ export { CommandEmpty, CommandEmptyProps };
@@ -0,0 +1,48 @@
1
+ import {
2
+ commandListContext
3
+ } from "./chunk-L6CSVKWR.js";
4
+ import {
5
+ __decorateClass,
6
+ blockComponentStyles
7
+ } from "./chunk-FB3RHUS6.js";
8
+
9
+ // src/internal/command/empty.ts
10
+ import { consume } from "@lit-labs/context";
11
+ import { LitElement, html } from "lit";
12
+ import { customElement, state } from "lit/decorators.js";
13
+ var CommandEmpty = class extends LitElement {
14
+ willUpdate(_changedProperties) {
15
+ var _a;
16
+ const scores = (_a = this.listContext) == null ? void 0 : _a.scores;
17
+ let hasMatch = false;
18
+ if (scores) {
19
+ for (const score of scores.values()) {
20
+ if (score > 0) {
21
+ hasMatch = true;
22
+ break;
23
+ }
24
+ }
25
+ }
26
+ this.hidden = hasMatch;
27
+ this.inert = hasMatch;
28
+ }
29
+ /** @hidden */
30
+ render() {
31
+ if (this.hidden) {
32
+ return null;
33
+ }
34
+ return html`<div role="option"><slot></slot></div>`;
35
+ }
36
+ };
37
+ /** @hidden */
38
+ CommandEmpty.styles = blockComponentStyles;
39
+ __decorateClass([
40
+ consume({ context: commandListContext, subscribe: true }),
41
+ state()
42
+ ], CommandEmpty.prototype, "listContext", 2);
43
+ CommandEmpty = __decorateClass([
44
+ customElement("prosekit-command-empty")
45
+ ], CommandEmpty);
46
+ export {
47
+ CommandEmpty
48
+ };
@@ -0,0 +1,30 @@
1
+ import * as lit from 'lit';
2
+ import { LitElement, CSSResultGroup, PropertyValues } from 'lit';
3
+ import { C as CommandListContext } from './list-context-2692a600.js';
4
+
5
+ interface CommandItemProps {
6
+ value?: string;
7
+ onSelect: VoidFunction;
8
+ }
9
+ /**
10
+ * Command menu item. Becomes active on pointer enter or through keyboard
11
+ * navigation. Preferably pass a `value`, otherwise the value will be inferred
12
+ * from the rendered item's `textContent`.
13
+ */
14
+ declare class CommandItem extends LitElement implements Partial<CommandItemProps> {
15
+ /** @hidden */
16
+ static styles: CSSResultGroup;
17
+ value: string;
18
+ selected: boolean;
19
+ /** @hidden */
20
+ onSelect?: VoidFunction;
21
+ /** @hidden */
22
+ defaultSlot?: HTMLSlotElement;
23
+ listContext?: CommandListContext;
24
+ get content(): string;
25
+ protected willUpdate(changedProperties: PropertyValues<CommandItem>): void;
26
+ /** @hidden */
27
+ render(): lit.TemplateResult<1> | null;
28
+ }
29
+
30
+ export { CommandItem, CommandItemProps };
@@ -0,0 +1,65 @@
1
+ import {
2
+ commandListContext
3
+ } from "./chunk-L6CSVKWR.js";
4
+ import {
5
+ __decorateClass,
6
+ blockComponentStyles
7
+ } from "./chunk-FB3RHUS6.js";
8
+
9
+ // src/internal/command/item.ts
10
+ import { consume } from "@lit-labs/context";
11
+ import { LitElement, html } from "lit";
12
+ import { customElement, property, query, state } from "lit/decorators.js";
13
+ var CommandItem = class extends LitElement {
14
+ constructor() {
15
+ super(...arguments);
16
+ this.value = "";
17
+ this.selected = false;
18
+ }
19
+ get content() {
20
+ const text = this.value || this.textContent || "";
21
+ return text.trim().toLowerCase();
22
+ }
23
+ willUpdate(changedProperties) {
24
+ var _a, _b;
25
+ const content = this.content;
26
+ this.selected = content === ((_a = this.listContext) == null ? void 0 : _a.selectedValue);
27
+ const score = ((_b = this.listContext) == null ? void 0 : _b.scores.get(content)) || 0;
28
+ this.inert = score <= 0;
29
+ this.hidden = score <= 0;
30
+ if (changedProperties.has("listContext") && this.listContext) {
31
+ this.listContext.registerValue(this.content);
32
+ }
33
+ }
34
+ /** @hidden */
35
+ render() {
36
+ if (this.hidden) {
37
+ return null;
38
+ }
39
+ return html`<div role="option" aria-selected="${this.selected}"><slot></slot></div>`;
40
+ }
41
+ };
42
+ /** @hidden */
43
+ CommandItem.styles = blockComponentStyles;
44
+ __decorateClass([
45
+ property({ type: String, reflect: true, attribute: "data-value" })
46
+ ], CommandItem.prototype, "value", 2);
47
+ __decorateClass([
48
+ property({ type: Boolean, reflect: true, attribute: "data-selected" })
49
+ ], CommandItem.prototype, "selected", 2);
50
+ __decorateClass([
51
+ property({ attribute: false })
52
+ ], CommandItem.prototype, "onSelect", 2);
53
+ __decorateClass([
54
+ query("slot")
55
+ ], CommandItem.prototype, "defaultSlot", 2);
56
+ __decorateClass([
57
+ consume({ context: commandListContext, subscribe: true }),
58
+ state({})
59
+ ], CommandItem.prototype, "listContext", 2);
60
+ CommandItem = __decorateClass([
61
+ customElement("prosekit-command-item")
62
+ ], CommandItem);
63
+ export {
64
+ CommandItem
65
+ };
@@ -0,0 +1,39 @@
1
+ import * as lit from 'lit';
2
+ import { LitElement, CSSResultGroup, PropertyValues } from 'lit';
3
+ import { Editor } from '@prosekit/core';
4
+ import { C as CommandListContext } from './list-context-2692a600.js';
5
+ import { C as CommandPopoverContext } from './popover-context-d5266bd6.js';
6
+
7
+ interface CommandListProps {
8
+ editor: Editor;
9
+ }
10
+ declare class CommandList extends LitElement implements Partial<CommandListProps> {
11
+ /** @hidden */
12
+ static styles: CSSResultGroup;
13
+ private controller;
14
+ private get active();
15
+ editor?: Editor;
16
+ popoverContext: CommandPopoverContext | null;
17
+ context: CommandListContext;
18
+ protected firstUpdated(): void;
19
+ private get items();
20
+ private get availableItems();
21
+ private get firstItem();
22
+ private get selectedItem();
23
+ selectFirstItem(): void;
24
+ private updateValue;
25
+ private registerValue;
26
+ /** @hidden */
27
+ defaultSlot?: HTMLSlotElement;
28
+ /** @hidden */
29
+ willUpdate(changedProperties: PropertyValues<CommandList>): void;
30
+ private updateSelectedByChange;
31
+ private handleMouseOver;
32
+ private handleClick;
33
+ private handleMouseDown;
34
+ private handleSelect;
35
+ /** @hidden */
36
+ render(): lit.TemplateResult<1>;
37
+ }
38
+
39
+ export { CommandList, CommandListProps };
@@ -0,0 +1,218 @@
1
+ import {
2
+ commandListContext
3
+ } from "./chunk-L6CSVKWR.js";
4
+ import {
5
+ commandPopoverContext,
6
+ isCommandItem
7
+ } from "./chunk-WPC5TQP7.js";
8
+ import {
9
+ __decorateClass,
10
+ blockComponentStyles
11
+ } from "./chunk-FB3RHUS6.js";
12
+
13
+ // src/internal/command/list.ts
14
+ import { consume, provide } from "@lit-labs/context";
15
+ import { html, LitElement } from "lit";
16
+ import { customElement, property, query, state } from "lit/decorators.js";
17
+
18
+ // src/internal/command-score.ts
19
+ import commandScoreModule from "@superhuman/command-score";
20
+ var commandScore = commandScoreModule;
21
+
22
+ // src/internal/command/list-controller.ts
23
+ import { addKeymap } from "@prosekit/core";
24
+ var CommandListController = class {
25
+ constructor(host, keymap) {
26
+ this.host = host;
27
+ this.keymap = keymap;
28
+ this.editor = null;
29
+ this.cleanup = null;
30
+ this.host.addController(this);
31
+ }
32
+ setEditor(editor) {
33
+ if (this.editor !== editor) {
34
+ this.editor = editor;
35
+ this.addExtension();
36
+ }
37
+ }
38
+ hostDisconnected() {
39
+ var _a;
40
+ (_a = this.cleanup) == null ? void 0 : _a.call(this);
41
+ this.cleanup = null;
42
+ }
43
+ addExtension() {
44
+ var _a;
45
+ (_a = this.cleanup) == null ? void 0 : _a.call(this);
46
+ this.cleanup = null;
47
+ if (!this.editor || !this.keymap) {
48
+ return;
49
+ }
50
+ const extension = addKeymap(this.keymap);
51
+ this.cleanup = this.editor.use(extension);
52
+ }
53
+ };
54
+
55
+ // src/internal/command/list.ts
56
+ var CommandList = class extends LitElement {
57
+ constructor() {
58
+ super(...arguments);
59
+ this.controller = new CommandListController(this, {
60
+ ArrowUp: () => {
61
+ if (!this.active)
62
+ return false;
63
+ this.updateSelectedByChange(-1);
64
+ return true;
65
+ },
66
+ ArrowDown: () => {
67
+ if (!this.active)
68
+ return false;
69
+ this.updateSelectedByChange(1);
70
+ return true;
71
+ },
72
+ Escape: () => {
73
+ var _a, _b;
74
+ if (!this.active)
75
+ return false;
76
+ (_b = (_a = this.popoverContext) == null ? void 0 : _a.handleDismiss) == null ? void 0 : _b.call(_a);
77
+ return true;
78
+ },
79
+ Enter: () => {
80
+ if (!this.active)
81
+ return false;
82
+ this.handleSelect(this.selectedItem);
83
+ return true;
84
+ }
85
+ });
86
+ this.popoverContext = null;
87
+ this.context = {
88
+ scores: /* @__PURE__ */ new Map(),
89
+ selectedValue: "",
90
+ registerValue: (value) => this.registerValue(value)
91
+ };
92
+ }
93
+ get active() {
94
+ var _a, _b;
95
+ return (_b = (_a = this.popoverContext) == null ? void 0 : _a.active) != null ? _b : false;
96
+ }
97
+ firstUpdated() {
98
+ this.selectFirstItem();
99
+ }
100
+ get items() {
101
+ var _a, _b, _c;
102
+ return (_c = (_b = (_a = this.defaultSlot) == null ? void 0 : _a.assignedElements({ flatten: true })) == null ? void 0 : _b.filter(isCommandItem)) != null ? _c : [];
103
+ }
104
+ get availableItems() {
105
+ var _a, _b;
106
+ return (_b = (_a = this.items) == null ? void 0 : _a.filter((item) => !item.hidden)) != null ? _b : [];
107
+ }
108
+ get firstItem() {
109
+ var _a;
110
+ return (_a = this.availableItems[0]) != null ? _a : null;
111
+ }
112
+ get selectedItem() {
113
+ var _a;
114
+ return (_a = this.availableItems.find(
115
+ (item) => item.content === this.context.selectedValue
116
+ )) != null ? _a : null;
117
+ }
118
+ selectFirstItem() {
119
+ var _a, _b;
120
+ const selected = (_b = (_a = this.firstItem) == null ? void 0 : _a.content) != null ? _b : "";
121
+ this.updateValue(selected);
122
+ }
123
+ updateValue(selectedValue) {
124
+ if (this.context.selectedValue === selectedValue)
125
+ return;
126
+ this.context = { ...this.context, selectedValue };
127
+ }
128
+ registerValue(value) {
129
+ if (!this.context.scores.has(value)) {
130
+ this.context.scores.set(value, 0);
131
+ this.requestUpdate();
132
+ }
133
+ return () => {
134
+ this.context.scores.delete(value);
135
+ };
136
+ }
137
+ /** @hidden */
138
+ willUpdate(changedProperties) {
139
+ var _a, _b;
140
+ if (changedProperties.has("editor") && this.editor) {
141
+ this.controller.setEditor(this.editor);
142
+ }
143
+ const query2 = (_b = (_a = this.popoverContext) == null ? void 0 : _a.query) != null ? _b : "";
144
+ const scores = new Map(
145
+ this.items.map((item) => {
146
+ const content = item.content;
147
+ const score = commandScore(content, query2);
148
+ return [content, score];
149
+ })
150
+ );
151
+ this.context = { ...this.context, scores };
152
+ }
153
+ updateSelectedByChange(change) {
154
+ const items = this.availableItems;
155
+ if (items.length === 0) {
156
+ return;
157
+ }
158
+ const selectedItem = this.selectedItem;
159
+ const selectedIndex = selectedItem ? items.indexOf(selectedItem) : -1;
160
+ let nextIndex = selectedIndex + change;
161
+ if (nextIndex < 0) {
162
+ nextIndex = 0;
163
+ } else if (nextIndex >= items.length) {
164
+ nextIndex = items.length - 1;
165
+ }
166
+ if (selectedIndex !== nextIndex) {
167
+ this.updateValue(items[nextIndex].content);
168
+ }
169
+ }
170
+ handleMouseOver(event) {
171
+ const target = event.target;
172
+ if (isCommandItem(target)) {
173
+ this.updateValue(target.content);
174
+ }
175
+ }
176
+ handleClick(event) {
177
+ event.preventDefault();
178
+ const target = event.target;
179
+ const item = target == null ? void 0 : target.closest("prosekit-command-item");
180
+ if (item && isCommandItem(item)) {
181
+ this.handleSelect(item);
182
+ }
183
+ }
184
+ handleMouseDown(event) {
185
+ event.preventDefault();
186
+ }
187
+ handleSelect(item) {
188
+ var _a, _b, _c;
189
+ (_b = (_a = this.popoverContext) == null ? void 0 : _a.handleSubmit) == null ? void 0 : _b.call(_a);
190
+ (_c = item == null ? void 0 : item.onSelect) == null ? void 0 : _c.call(item);
191
+ }
192
+ /** @hidden */
193
+ render() {
194
+ return html`<div role="listbox" aria-label="Suggestions" @mouseover="${this.handleMouseOver.bind(this)}" @click="${this.handleClick.bind(this)}" @mousedown="${this.handleMouseDown.bind(this)}"><slot></slot></div>`;
195
+ }
196
+ };
197
+ /** @hidden */
198
+ CommandList.styles = blockComponentStyles;
199
+ __decorateClass([
200
+ property({ attribute: false })
201
+ ], CommandList.prototype, "editor", 2);
202
+ __decorateClass([
203
+ consume({ context: commandPopoverContext, subscribe: true }),
204
+ state()
205
+ ], CommandList.prototype, "popoverContext", 2);
206
+ __decorateClass([
207
+ provide({ context: commandListContext }),
208
+ state()
209
+ ], CommandList.prototype, "context", 2);
210
+ __decorateClass([
211
+ query("slot")
212
+ ], CommandList.prototype, "defaultSlot", 2);
213
+ CommandList = __decorateClass([
214
+ customElement("prosekit-command-list")
215
+ ], CommandList);
216
+ export {
217
+ CommandList
218
+ };
@@ -0,0 +1,51 @@
1
+ import * as lit from 'lit';
2
+ import { LitElement, CSSResultGroup, PropertyValues } from 'lit';
3
+ import { ComputePositionConfig } from '@floating-ui/dom';
4
+ import { Editor } from '@prosekit/core';
5
+ import { C as CommandPopoverContext } from './popover-context-d5266bd6.js';
6
+
7
+ /**
8
+ * The `PopoverOptions` interface defines the options that can be passed to the
9
+ * `computePosition` function from Floating UI. These options are used to
10
+ * configure the positioning of the popover element relative to its reference
11
+ * element. For more information on the available options, please refer to the
12
+ * Floating UI documentation.
13
+ *
14
+ * https://floating-ui.com/docs/computeposition#options
15
+ */
16
+ type PopoverOptions = ComputePositionConfig;
17
+
18
+ type QueryBuilder = (match: RegExpMatchArray, matchAfter?: RegExpMatchArray | null) => string;
19
+
20
+ interface CommandPopoverProps {
21
+ editor: Editor;
22
+ regex: RegExp;
23
+ regexAfter?: RegExp;
24
+ queryBuilder?: QueryBuilder;
25
+ popoverOptions?: PopoverOptions;
26
+ }
27
+ declare class CommandPopover extends LitElement implements Partial<CommandPopoverProps> {
28
+ /** @hidden */
29
+ static styles: CSSResultGroup;
30
+ private controller;
31
+ editor?: Editor;
32
+ regex?: RegExp;
33
+ regexAfter?: RegExp;
34
+ queryBuilder: QueryBuilder;
35
+ popoverOptions: PopoverOptions;
36
+ context: CommandPopoverContext;
37
+ /** @hidden */
38
+ onSelect?: VoidFunction;
39
+ private get list();
40
+ private updateContext;
41
+ /** @hidden */
42
+ defaultSlot?: HTMLSlotElement;
43
+ /** @hidden */
44
+ protected get active(): boolean;
45
+ /** @hidden */
46
+ willUpdate(changedProperties: PropertyValues<CommandPopover>): void;
47
+ /** @hidden */
48
+ render(): lit.TemplateResult<1>;
49
+ }
50
+
51
+ export { CommandPopover, CommandPopoverProps, PopoverOptions, QueryBuilder };