@draftably/solid 0.4.0 → 0.6.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
@@ -5,13 +5,14 @@ Draftably diagrams in Solid. `DraftDiagram` wraps the `<draft-diagram>` element
5
5
  cleanup when it unmounts. Any number of diagrams can share a page.
6
6
 
7
7
  ```sh
8
- pnpm add @draftably/solid
8
+ pnpm add @draftably/solid @draftably/runtime
9
9
  ```
10
10
 
11
11
  Import `@draftably/runtime/host.css` once in your client entry: it reserves each element's height
12
- and hides slot content until the element loads. With Vite, add
13
- `optimizeDeps: { exclude: ['@draftably/runtime', '@draftably/editor'] }` to `vite.config`.
12
+ and hides slot content until the element loads, and it lets marked content take the pointer. With
13
+ Vite 5–7, add `optimizeDeps: { exclude: ['@draftably/runtime', '@draftably/editor'] }` to
14
+ `vite.config`.
14
15
 
15
- The [Solid recipe](https://draftably.dev/docs/frameworks#solid) is a complete, type-checked app: both
16
- components, an event, your content on a node, a dark theme and server rendering with the `svg`
17
- prop. CI compiles it from the packed packages.
16
+ [Solid and SolidStart](https://draftably.dev/docs/solid) has a complete, type-checked app: both components,
17
+ an event, your content on a node, a dark page and server rendering with the `svg` prop. CI
18
+ compiles it from the packed packages. The same page has the SolidStart server and strict-CSP code.
package/dist/editor.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  /** `<DraftEditor>` for Solid: drag boxes and lines, and get the `.draft` source back. */
2
2
  import type { DraftEditorElement, EditorOptions } from '@draftably/editor/element';
3
3
  import { type JSX } from 'solid-js';
4
- import { type HostEventProps } from './events.js';
4
+ import { type ElementAttributes, type HostEventProps } from './events.js';
5
5
  export type { DraftEditorElement, EditorOptions };
6
6
  declare module 'solid-js' {
7
7
  namespace JSX {
@@ -13,7 +13,8 @@ declare module 'solid-js' {
13
13
  }
14
14
  }
15
15
  }
16
- export interface DraftEditorProps extends HostEventProps {
16
+ /** The element's props, its event props, and any other attribute, such as `data-theme`. */
17
+ export interface DraftEditorProps extends HostEventProps, ElementAttributes {
17
18
  /** `.draft` source. Passing back the source from `onChange` does not redraw. */
18
19
  source: string;
19
20
  /** Every `mountEditor` option but `source` and `onChange`. Callbacks apply live. */
package/dist/editor.jsx CHANGED
@@ -1,6 +1,8 @@
1
- import { onMount } from 'solid-js';
2
- import { listenHostEvents } from './events.js';
1
+ import { onMount, splitProps } from 'solid-js';
2
+ import { elementAttributes, EVENT_PROPS, listenHostEvents, } from './events.js';
3
+ const OWN = ['source', 'options', 'ref', 'class', 'style', 'children'];
3
4
  export function DraftEditor(props) {
5
+ const [, , rest] = splitProps(props, OWN, EVENT_PROPS);
4
6
  let el;
5
7
  // The editor loads in the browser only, so server rendering and viewer-only pages skip it.
6
8
  onMount(() => import('@draftably/editor/element').catch((error) => {
@@ -15,7 +17,7 @@ export function DraftEditor(props) {
15
17
  // Only when set: Solid's server HTML writes `style=""` for undefined, which a strict CSP blocks.
16
18
  {...(props.style === undefined ? {} : { style: props.style })}
17
19
  // Only when set, so options a host set through the ref survive.
18
- {...(props.options === undefined ? {} : { 'prop:options': props.options })}>
20
+ {...(props.options === undefined ? {} : { 'prop:options': props.options })} {...elementAttributes(rest)}>
19
21
  {props.children}
20
22
  </draft-editor>);
21
23
  }
package/dist/events.d.ts CHANGED
@@ -1,9 +1,22 @@
1
1
  /** Event props from the runtime's one list of events: a new event adds no lines here. */
2
- import { type HostEventName, type HostEvents } from '@draftably/runtime/host';
3
- export type { HostEventName, HostEvents };
4
- /** `onCamera`, `onSelect`, … : each receives the event's `detail`. */
2
+ import { type HostEventDetail, type HostEventName, type HostEvents } from '@draftably/runtime/host';
3
+ import type { JSX } from 'solid-js';
4
+ export type { HostEventDetail, HostEventName, HostEvents };
5
+ /**
6
+ * `onCamera`, `onSelect`, … : each receives the event's `detail`. `onLink` and `onContextmenu`
7
+ * also get `preventDefault()`.
8
+ */
5
9
  export type HostEventProps = {
6
- [K in HostEventName as `on${Capitalize<K>}`]?: (detail: HostEvents[K]) => void;
10
+ [K in HostEventName as `on${Capitalize<K>}`]?: (detail: HostEventDetail<K>) => void;
7
11
  };
12
+ /** The event prop names, which the components keep off the element. */
13
+ export declare const EVENT_PROPS: ("onAnchors" | "onUpdate" | "onRender" | "onDiagnostics" | "onCamera" | "onItemactivate" | "onLink" | "onContextmenu" | "onSelect" | "onChange")[];
8
14
  /** Listen for every host event on `node`; each call reads the latest prop. */
9
15
  export declare function listenHostEvents(node: HTMLElement, props: HostEventProps): void;
16
+ /** The DOM attributes a component passes to its element, such as `data-theme`, `id` or `aria-*`. */
17
+ export type ElementAttributes = Omit<JSX.HTMLAttributes<HTMLElement>, keyof HostEventProps | 'children' | 'ref' | 'class' | 'style'>;
18
+ /**
19
+ * Writes hyphenated names such as `data-theme` as attributes. On a custom element, Solid's spread
20
+ * sets every other name as a property, so `data-theme` would become `el.dataTheme`.
21
+ */
22
+ export declare function elementAttributes(rest: object): Record<string, unknown>;
package/dist/events.js CHANGED
@@ -1,9 +1,18 @@
1
1
  /** Event props from the runtime's one list of events: a new event adds no lines here. */
2
- import { HOST_EVENTS } from '@draftably/runtime/host';
2
+ import { HOST_EVENTS, hostDetail, } from '@draftably/runtime/host';
3
+ /** The event prop names, which the components keep off the element. */
4
+ export const EVENT_PROPS = HOST_EVENTS.map(name => `on${name[0].toUpperCase()}${name.slice(1)}`);
3
5
  /** Listen for every host event on `node`; each call reads the latest prop. */
4
6
  export function listenHostEvents(node, props) {
5
7
  for (const name of HOST_EVENTS) {
6
8
  const key = `on${name[0].toUpperCase()}${name.slice(1)}`;
7
- node.addEventListener(`draft-${name}`, event => props[key]?.(event.detail));
9
+ node.addEventListener(`draft-${name}`, event => props[key]?.(hostDetail(event)));
8
10
  }
9
11
  }
12
+ /**
13
+ * Writes hyphenated names such as `data-theme` as attributes. On a custom element, Solid's spread
14
+ * sets every other name as a property, so `data-theme` would become `el.dataTheme`.
15
+ */
16
+ export function elementAttributes(rest) {
17
+ return Object.fromEntries(Object.entries(rest).map(([key, value]) => [key.includes('-') ? `attr:${key}` : key, value]));
18
+ }
package/dist/index.d.ts CHANGED
@@ -3,10 +3,10 @@ import '@draftably/runtime/element';
3
3
  import type { DraftDiagramElement } from '@draftably/runtime/element';
4
4
  import type { UpdateResult, ViewerOptions } from '@draftably/runtime/viewer';
5
5
  import { type JSX } from 'solid-js';
6
- import { type HostEventProps } from './events.js';
6
+ import { type ElementAttributes, type HostEventProps } from './events.js';
7
7
  export { renderSvg } from '@draftably/runtime';
8
8
  export { HOST_EVENTS } from '@draftably/runtime/host';
9
- export type { HostEventName, HostEventProps, HostEvents } from './events.js';
9
+ export type { ElementAttributes, HostEventDetail, HostEventName, HostEventProps, HostEvents, } from './events.js';
10
10
  export type { DraftDiagramElement, UpdateResult, ViewerOptions };
11
11
  declare module 'solid-js' {
12
12
  namespace JSX {
@@ -21,7 +21,8 @@ declare module 'solid-js' {
21
21
  }
22
22
  }
23
23
  }
24
- export interface DraftDiagramProps extends HostEventProps {
24
+ /** The element's props, its event props, and any other attribute, such as `data-theme`. */
25
+ export interface DraftDiagramProps extends HostEventProps, ElementAttributes {
25
26
  /** `.draft` source. A change redraws the diagram and keeps the camera. */
26
27
  source: string;
27
28
  /** A declared emphasis state to show. */
package/dist/index.jsx CHANGED
@@ -1,10 +1,23 @@
1
1
  /** `<DraftDiagram>` for Solid: the `<draft-diagram>` element with Solid props, a ref and event props. */
2
2
  import '@draftably/runtime/element';
3
- import { Show } from 'solid-js';
4
- import { listenHostEvents } from './events.js';
3
+ import { Show, splitProps } from 'solid-js';
4
+ import { elementAttributes, EVENT_PROPS, listenHostEvents, } from './events.js';
5
5
  export { renderSvg } from '@draftably/runtime';
6
6
  export { HOST_EVENTS } from '@draftably/runtime/host';
7
+ const OWN = [
8
+ 'source',
9
+ 'state',
10
+ 'label',
11
+ 'outline',
12
+ 'svg',
13
+ 'options',
14
+ 'ref',
15
+ 'class',
16
+ 'style',
17
+ 'children',
18
+ ];
7
19
  export function DraftDiagram(props) {
20
+ const [, , rest] = splitProps(props, OWN, EVENT_PROPS);
8
21
  return (<draft-diagram ref={(node) => {
9
22
  listenHostEvents(node, props);
10
23
  props.ref?.(node);
@@ -12,7 +25,7 @@ export function DraftDiagram(props) {
12
25
  // Only when set: Solid's server HTML writes `style=""` for undefined, which a strict CSP blocks.
13
26
  {...(props.style === undefined ? {} : { style: props.style })}
14
27
  // Only when set, so options a host set through the ref survive.
15
- {...(props.options === undefined ? {} : { 'prop:options': props.options })}>
28
+ {...(props.options === undefined ? {} : { 'prop:options': props.options })} {...elementAttributes(rest)}>
16
29
  <Show when={props.svg}>{svg => <div innerHTML={svg()}/>}</Show>
17
30
  {props.children}
18
31
  </draft-diagram>);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@draftably/solid",
3
- "version": "0.4.0",
3
+ "version": "0.6.0",
4
4
  "description": "Solid components for Draftably diagrams.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -23,8 +23,8 @@
23
23
  },
24
24
  "sideEffects": false,
25
25
  "dependencies": {
26
- "@draftably/runtime": "0.4.0",
27
- "@draftably/editor": "0.4.0"
26
+ "@draftably/runtime": "0.6.0",
27
+ "@draftably/editor": "0.6.0"
28
28
  },
29
29
  "peerDependencies": {
30
30
  "solid-js": "^1.8"