playhtml 1.3.0 → 2.0.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/CHANGELOG.md CHANGED
@@ -2,6 +2,40 @@
2
2
 
3
3
  The format is based on Keep a Changelog and this project adheres to Semantic Versioning.
4
4
 
5
+ ## 2.0.0 - 2023-08-23
6
+
7
+ - **BREAKING CHANGE**: Changed the initializing of playhtml to be an explicit call of `playhtml.init()` from just a normal import. You can still use the old code if you pin the import to any version 1.3.1 (e.g. use `https://unpkg.com/playhtml@1.3.1` as the import source).
8
+
9
+ **OLD CODE:**
10
+
11
+ ```html
12
+ <script type="module" src="https://unpkg.com/playhtml"></script>
13
+ <link rel="stylesheet" href="https://unpkg.com/playhtml/dist/style.css" />
14
+ ```
15
+
16
+ **NEW CODE:**
17
+
18
+ ```html
19
+ <script type="module">
20
+ import "https://unpkg.com/playhtml";
21
+ playhtml.init();
22
+ // Optionally you could call
23
+ // playhtml.init({
24
+ // room: window.location.pathname,
25
+ // host: "mypartykit.user.partykit.dev"
26
+ // })
27
+ </script>
28
+ <link rel="stylesheet" href="https://unpkg.com/playhtml/dist/style.css" />
29
+ ```
30
+
31
+ This change allows for more flexible use of the package, including specifying a partykit host and room.
32
+
33
+ - was accidentally importing all my files for the website into the package, blowing it up to 4MB. I've fixed this and compressed down the `.d.ts` types file to just what is needed, so the package is down to 360KB. It should load much faster on websites now :)
34
+
35
+ ## 1.3.1 - 2023-08-09
36
+
37
+ - Removed unused code and consolidated types in `types.ts`
38
+
5
39
  ## 1.3.0 - 2023-08-07
6
40
 
7
41
  - Added support for `can-duplicate` capability to duplicate elements. Make factories for playhtml elements!!
package/README.md CHANGED
@@ -27,7 +27,15 @@ To use this library, you can import the library and the associated styles from a
27
27
  id="openSign"
28
28
  <!-- INVALID EXAMPLE <img src="https://media2.giphy.com/media/lL7A3Li0YtFHq/giphy.gif?cid=ecf05e47ah89o71gzz7ke7inrgb1ai1xcbrjnqdf7o890118&ep=v1_stickers_search&rid=giphy.gif" can-move /> -->
29
29
  <!-- import the script -->
30
- <script type="module" src="https://unpkg.com/playhtml"></script>
30
+ <script type="module">
31
+ import "https://unpkg.com/playhtml";
32
+ playhtml.init();
33
+ // Optionally you could customize the room and host, like so:
34
+ // playhtml.init({
35
+ // room: window.location.pathname,
36
+ // host: "mypartykit.user.partykit.dev"
37
+ // })
38
+ </script>
31
39
  <link rel="stylesheet" href="https://unpkg.com/playhtml/dist/style.css" />
32
40
  </body>
33
41
  ```
@@ -66,8 +74,15 @@ https://github.com/spencerc99/playhtml/assets/14796580/fae669b1-b3e2-404e-bd7a-3
66
74
  <img can-play id="customCandle" src="/candle-gif.gif" />
67
75
  <!-- IMPORTANT: this data must be set _before_ importing the playhtml library. -->
68
76
  <script>
69
- // Every HTML element with an ID has a variable corresponding to that ID name in the global context (see https://stackoverflow.com/questions/3434278/do-dom-tree-elements-with-ids-become-global-properties/3434388#3434388)
70
- // This is fun, magical, and readable for small projects, but if you have a larger, complex project, I would advise using `document.getElementById` or `document.querySelector`...
77
+ let candle = document.getElementById("customCandle");
78
+ candle.defaultData = true;
79
+ candle.onClick = (_e, { data, setData }) => {
80
+ setData(!data);
81
+ };
82
+ candle.updateElement = ({ data }) => {
83
+ candle.src = data ? "/candle-gif.gif" : "/candle-off.png";
84
+ };
85
+ candle.resetShortcut = "shiftKey";
71
86
  customCandle.defaultData = true;
72
87
  customCandle.onClick = (_e, { data, setData }) => {
73
88
  setData(!data);
@@ -80,37 +95,26 @@ https://github.com/spencerc99/playhtml/assets/14796580/fae669b1-b3e2-404e-bd7a-3
80
95
  // setData(!data);
81
96
  // });
82
97
  // };
83
- customCandle.updateElement = ({ data }) => {
84
- customCandle.src = data ? "/candle-gif.gif" : "/candle-off.png";
85
- };
86
-
87
- // Alternatively
88
- // let candle = document.getElementById('customCandle');
89
- // candle.defaultData = true;
90
- // candle.onClick = (_e, { data, setData }) => {
91
- // setData(!data);
92
- // };
93
- // candle.updateElement = ({ data }) => {
94
- // candle.src = data ? "/candle-gif.gif" : "/candle-off.png";
95
- // };
96
- // candle.resetShortcut = "shiftKey";
97
98
  </script>
98
99
 
99
100
  <!-- Import playhtml -->
100
- <script type="module" src="https://unpkg.com/playhtml"></script>
101
+ <script type="module">
102
+ import "https://unpkg.com/playhtml";
103
+ playhtml.init();
104
+ </script>
101
105
  <link rel="stylesheet" href="https://unpkg.com/playhtml/dist/style.css" />
102
106
  ```
103
107
 
104
- The only required properties are `defaultData`, `updateElement` and some kind of setup to trigger those functions (in this case, `onClick`, but you can add custom event listeners and logic using the `additionalSetup` property). See more examples based on the definitions for the included capabilities in `elements.ts`.
108
+ See all supported properties in the `ElementInitializer` [object in `types.ts`](https://github.com/spencerc99/playhtml/blob/main/src/types.ts#L13).
105
109
 
106
- If you make something fun, please show me! This is designed as an open library for anyone to add on new interactions and capabilities, so I'd love to host your custom elements here as well.
110
+ The only required properties are `defaultData`, `updateElement` and some kind of setup to trigger those functions (in this case, `onClick`, but you can add custom event listeners and logic using the `additionalSetup` property). See more examples based on the definitions for the included capabilities in [`elements.ts`](https://github.com/spencerc99/playhtml/blob/main/src/elements.ts#L72).
111
+
112
+ If you make something fun, please show me! This is designed as an open library for anyone to add on new interactions and capabilities, so we [welcome contributions](https://github.com/spencerc99/playhtml/blob/main/CONTRIBUTING.md) for new built-in capabilities.
107
113
 
108
114
  ## Plug-and-play Capabilities
109
115
 
110
116
  These capabilities are common ones that have been designed and created by the community. You should expect that they are relatively well-tested, and they simply build on top of the same API and constructs that `can-play` uses.
111
117
 
112
- We welcome any contributions if you have custom `can-play` elements that you would like to add to the library! Please make a PR.
113
-
114
118
  ### `can-move`
115
119
 
116
120
  https://github.com/spencerc99/playhtml/assets/14796580/9c2b9bf6-142c-41e2-8c8f-93a3b121a73e
@@ -146,21 +150,9 @@ Creates a rotatable element using a `rotate` `transform` on the element. Draggin
146
150
 
147
151
  Creates a communal forum from a `form` element. The form will sync any new submissions including all the `input` elements in the form, using their `name` property as the key and their value as the value. New messages will be currently prepended to the element with the `guestbookMessages` ID. TODO: make this generic and take user input
148
152
 
149
- ---
150
-
151
- To add new ones, please find contributor guidelines in [New Capabilities](#new-capabilities).
152
-
153
153
  ## Contributing
154
154
 
155
- ### New Capabilities
156
-
157
- `playhtml` is designed to be a collective library of magical capabilities that anyone can attach to arbitrary HTML elements. If you have an idea for a new capability, please first ensure that there is not a duplicate existing one in the current library (see `TagType` in `types.ts`). Please make a proposal for the capability you would like to add by opening an issue with the `new-capability` label.
158
-
159
- To contribute your capability, see sample PR (TODO: LINK).
160
-
161
- ---
162
-
163
- Outside of contributing new capabilities, feel free to submit any issues or PRs for bugs or improvements to the core of the library.
155
+ See [CONTRIBUTING.md](https://github.com/spencerc99/playhtml/blob/main/CONTRIBUTING.md).
164
156
 
165
157
  ## Support & Maintenance
166
158
 
package/dist/main.d.ts CHANGED
@@ -1,23 +1,138 @@
1
- /// <reference lib="dom" />
2
- import { TagType } from "./types";
3
- import * as Y from "yjs";
4
- import { ElementHandler } from "./elements";
5
- export declare function getElementFromId(id: string): HTMLElement | null;
6
- /**
7
- * Sets up any playhtml elements that are currently on the page.
8
- *
9
- * Should be called only once. If you'd like to set up new elements, use `setupPlayElement`, which is exposed
10
- * on the `playhtml` object on `window`.
11
- */
12
- export declare function setupElements(): void;
13
- export declare const playhtml: {
14
- setupElements: typeof setupElements;
15
- globalData: Y.Map<Y.Map<any>>;
16
- elementHandlers: Map<string, Map<string, ElementHandler<any, any, any>>>;
17
- setupPlayElement: typeof setupPlayElement;
18
- };
19
- /**
20
- * Sets up a playhtml element to handle the given tag's capabilities.
21
- */
22
- export declare function setupPlayElementForTag<T extends TagType>(element: HTMLElement, tag: T): void;
23
- export declare function setupPlayElement(element: Element): void;
1
+ /// <reference lib="dom" />
2
+
3
+ import * as Y from 'yjs';
4
+
5
+ declare interface ElementAwarenessEventHandlerData<T = any, U = any, V = any> extends ElementEventHandlerData<T, U, V> {
6
+ myAwareness?: V;
7
+ }
8
+
9
+ declare interface ElementData<T = any, U = any, V = any> extends ElementInitializer<T> {
10
+ data?: T;
11
+ localData?: U;
12
+ awareness?: V;
13
+ element: HTMLElement;
14
+ onChange: (data: T) => void;
15
+ onAwarenessChange: (data: V) => void;
16
+ triggerAwarenessUpdate: () => void;
17
+ }
18
+
19
+ declare interface ElementEventHandlerData<T = any, U = any, V = any> {
20
+ data: T;
21
+ localData: U;
22
+ awareness: V[];
23
+ element: HTMLElement;
24
+ setData: (data: T) => void;
25
+ setLocalData: (data: U) => void;
26
+ setLocalAwareness: (data: V) => void;
27
+ }
28
+
29
+ declare class ElementHandler<T = any, U = any, V = any> {
30
+ defaultData: T;
31
+ localData: U;
32
+ awareness: V[];
33
+ selfAwareness?: V;
34
+ element: HTMLElement;
35
+ _data: T;
36
+ onChange: (data: T) => void;
37
+ onAwarenessChange: (data: V) => void;
38
+ debouncedOnChange: (data: T) => void;
39
+ resetShortcut?: ModifierKey;
40
+ updateElement: (data: ElementEventHandlerData<T, U, V>) => void;
41
+ updateElementAwareness?: (data: ElementAwarenessEventHandlerData<T, U, V>) => void;
42
+ triggerAwarenessUpdate?: () => void;
43
+ constructor({ element, onChange, onAwarenessChange, defaultData, defaultLocalData, myDefaultAwareness, data, awareness: awarenessData, updateElement, updateElementAwareness, onClick, onDrag, onDragStart, additionalSetup, resetShortcut, debounceMs, triggerAwarenessUpdate, }: ElementData<T>);
44
+ get data(): T;
45
+ setLocalData(localData: U): void;
46
+ /**
47
+ * // PRIVATE USE ONLY \\
48
+ *
49
+ * Updates the internal state with the given data and handles all the downstream effects. Should only be used by the sync code to ensure one-way
50
+ * reactivity.
51
+ * (e.g. calling `updateElement` and `onChange`)
52
+ */
53
+ set __data(data: T);
54
+ set __awareness(data: V[]);
55
+ getEventHandlerData(): ElementEventHandlerData<T, U, V>;
56
+ getAwarenessEventHandlerData(): ElementAwarenessEventHandlerData<T, U, V>;
57
+ getSetupData(): ElementSetupData<T, U>;
58
+ /**
59
+ * Public-use setter for data that makes the change to all clients.
60
+ */
61
+ setData(data: T): void;
62
+ setLocalAwareness(data: V): void;
63
+ setDataDebounced(data: T): void;
64
+ /**
65
+ * Resets the element to its default state.
66
+ */
67
+ reset(): void;
68
+ }
69
+
70
+ declare interface ElementInitializer<T = any, U = any, V = any> {
71
+ defaultData: T | ((element: HTMLElement) => T);
72
+ defaultLocalData?: U | ((element: HTMLElement) => U);
73
+ myDefaultAwareness?: V | ((element: HTMLElement) => V);
74
+ updateElement: (data: ElementEventHandlerData<T, U, V>) => void;
75
+ updateElementAwareness?: (data: ElementAwarenessEventHandlerData<T, U, V>) => void;
76
+ onDrag?: (e: MouseEvent | TouchEvent, eventData: ElementEventHandlerData<T, U, V>) => void;
77
+ onClick?: (e: MouseEvent, eventData: ElementEventHandlerData<T, U, V>) => void;
78
+ onDragStart?: (e: MouseEvent | TouchEvent, eventData: ElementEventHandlerData<T, U, V>) => void;
79
+ additionalSetup?: (eventData: ElementSetupData<T, U, V>) => void;
80
+ resetShortcut?: ModifierKey;
81
+ debounceMs?: number;
82
+ }
83
+
84
+ declare interface ElementSetupData<T = any, U = any, V = any> {
85
+ getData: () => T;
86
+ getLocalData: () => U;
87
+ getAwareness: () => V[];
88
+ getElement: () => HTMLElement;
89
+ setData: (data: T) => void;
90
+ setLocalData: (data: U) => void;
91
+ setLocalAwareness: (data: V) => void;
92
+ }
93
+
94
+ declare interface InitOptions {
95
+ room?: string;
96
+ host?: string;
97
+ }
98
+
99
+ export declare function initPlayHTML({ room, host, }?: InitOptions): void;
100
+
101
+ declare type ModifierKey = "ctrlKey" | "altKey" | "shiftKey" | "metaKey";
102
+
103
+ export declare const playhtml: PlayHTMLComponents;
104
+
105
+ declare interface PlayHTMLComponents {
106
+ init: typeof initPlayHTML;
107
+ setupElements: typeof setupElements;
108
+ setupPlayElement: typeof setupPlayElement;
109
+ globalData: Y.Map<any> | undefined;
110
+ elementHandlers: Map<string, Map<string, ElementHandler>> | undefined;
111
+ }
112
+
113
+ /**
114
+ * Sets up any playhtml elements that are currently on the page.
115
+ *
116
+ * Should be called only once. If you'd like to set up new elements, use `setupPlayElement`, which is exposed
117
+ * on the `playhtml` object on `window`.
118
+ */
119
+ export declare function setupElements(): void;
120
+
121
+ export declare function setupPlayElement(element: Element): void;
122
+
123
+ /**
124
+ * Sets up a playhtml element to handle the given tag's capabilities.
125
+ */
126
+ export declare function setupPlayElementForTag<T extends TagType>(element: HTMLElement, tag: T): void;
127
+
128
+ declare enum TagType {
129
+ "CanPlay" = "can-play",
130
+ "CanMove" = "can-move",
131
+ "CanSpin" = "can-spin",
132
+ "CanGrow" = "can-grow",
133
+ "CanToggle" = "can-toggle",
134
+ "CanDuplicate" = "can-duplicate",
135
+ "CanPost" = "can-post"
136
+ }
137
+
138
+ export { }