playhtml 1.2.0 → 1.3.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.
package/CHANGELOG.md CHANGED
@@ -2,6 +2,10 @@
2
2
 
3
3
  The format is based on Keep a Changelog and this project adheres to Semantic Versioning.
4
4
 
5
+ ## 1.3.0 - 2023-08-07
6
+
7
+ - Added support for `can-duplicate` capability to duplicate elements. Make factories for playhtml elements!!
8
+
5
9
  ## 1.2.0 - 2023-08-03
6
10
 
7
11
  - Added support for yjs's `awareness` protocol to handle synced data that shouldn't be persisted.
package/README.md CHANGED
@@ -8,8 +8,6 @@ playhtml is a library for magically creating collaborative interactive HTML elem
8
8
  <div id="couch" can-move style="font-size: 80px">🛋</div>
9
9
  ```
10
10
 
11
- This is designed as an open library for anyone to add on new interactions and capabilities. To get started, see the [New Capabilities](#new-capabilities).
12
-
13
11
  https://github.com/spencerc99/playhtml/assets/14796580/00e84e15-2c1c-4b4b-8e15-2af22f39db7a
14
12
 
15
13
  If you enjoy this, please consider [sponsoring the project or sending a small donation](https://github.com/sponsors/spencerc99). This helps ensure that the library is maintained and improved over time and funds the hosting costs for the syncing and persistence services.
@@ -44,9 +42,11 @@ If you have dynamic elements that are hydrated after the initial load, you can c
44
42
 
45
43
  To create your own custom element, refer to the [can-play](#can-play) section.
46
44
 
45
+ If you're trying this out and having trouble, please message me ([email](mailto:spencerc99@gmail.com), [twitter](https://twitter.com/spencerc99)) and I'm happy to help out!
46
+
47
47
  ## Examples
48
48
 
49
- Check out the [full gallery of community examples](https://gallery.playhtml.fun/) for more inspiration on what you can do! And [submit your own](https://coda.io/form/playhtml-example_dnUR7xNE7wz) once you've made one.
49
+ Check out the [full gallery of community examples](https://coda.io/@spencer/playhtml) for more inspiration on what you can do! And [submit your own](https://coda.io/form/playhtml-example_dnUR7xNE7wz) once you've made one.
50
50
 
51
51
  ## Capabilities
52
52
 
@@ -66,8 +66,15 @@ https://github.com/spencerc99/playhtml/assets/14796580/fae669b1-b3e2-404e-bd7a-3
66
66
  <img can-play id="customCandle" src="/candle-gif.gif" />
67
67
  <!-- IMPORTANT: this data must be set _before_ importing the playhtml library. -->
68
68
  <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`...
69
+ let candle = document.getElementById("customCandle");
70
+ candle.defaultData = true;
71
+ candle.onClick = (_e, { data, setData }) => {
72
+ setData(!data);
73
+ };
74
+ candle.updateElement = ({ data }) => {
75
+ candle.src = data ? "/candle-gif.gif" : "/candle-off.png";
76
+ };
77
+ candle.resetShortcut = "shiftKey";
71
78
  customCandle.defaultData = true;
72
79
  customCandle.onClick = (_e, { data, setData }) => {
73
80
  setData(!data);
@@ -80,20 +87,6 @@ https://github.com/spencerc99/playhtml/assets/14796580/fae669b1-b3e2-404e-bd7a-3
80
87
  // setData(!data);
81
88
  // });
82
89
  // };
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
90
  </script>
98
91
 
99
92
  <!-- Import playhtml -->
@@ -101,38 +94,44 @@ https://github.com/spencerc99/playhtml/assets/14796580/fae669b1-b3e2-404e-bd7a-3
101
94
  <link rel="stylesheet" href="https://unpkg.com/playhtml/dist/style.css" />
102
95
  ```
103
96
 
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`.
97
+ See all supported properties in the `ElementInitializer` [object in `types.ts`](https://github.com/spencerc99/playhtml/blob/main/src/types.ts#L13).
98
+
99
+ 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).
100
+
101
+ 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.
105
102
 
106
103
  ## Plug-and-play Capabilities
107
104
 
108
105
  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.
109
106
 
110
- We welcome any contributions if you have custom `can-play` elements that you would like to add to the library! Please make a PR.
111
-
112
107
  ### `can-move`
113
108
 
114
109
  https://github.com/spencerc99/playhtml/assets/14796580/9c2b9bf6-142c-41e2-8c8f-93a3b121a73e
115
110
 
116
- Creates a movable object using 2D `translate` on the element. Dragging the element around will move it
111
+ Creates a movable element using 2D `translate` on the element. Dragging the element around will move it
117
112
 
118
113
  **troubleshooting**
119
114
 
120
115
  - This currently doesn't work on `inline` display elements.
121
116
  - This currently doesn't work on touch screens.
122
117
 
123
- ### `can-spin`
124
-
125
- Creates a rotatable object using a `rotate` `transform` on the element. Dragging the element to the left or right will rotate it counter-clockwise and clockwise respectively.
126
-
127
118
  ### `can-toggle`
128
119
 
129
120
  <blockquote class="twitter-tweet"><p lang="en" dir="ltr">today i installed some lamps on the demos-and-chill website<br><br>then <a href="https://twitter.com/_jzhao?ref_src=twsrc%5Etfw">@_jzhao</a> and i fought on whether to keep them on or not. <a href="https://t.co/sCspTwmRpS">pic.twitter.com/sCspTwmRpS</a></p>&mdash; spencer chang ☀️ (spencerchang.me @ bsky) (@spencerc99) <a href="https://twitter.com/spencerc99/status/1681048824884895744?ref_src=twsrc%5Etfw">July 17, 2023</a></blockquote> <script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>
130
121
 
131
- Creates an object that can be switched on and off. Clicking the element will toggle the `clicked` class on the element.
122
+ Creates an element that can be switched on and off. Clicking the element will toggle the `clicked` class on the element.
123
+
124
+ ### `can-duplicate`
125
+
126
+ Creates an element that duplicates a target element (specified by the value of the `can-duplicate` attribute, which can be an element's ID or custom CSS selector) when clicked. Optionally can specify where the duplicate element is inserted in the DOM via the `can-duplicate-to` setting (default is as a sibling to the original element).
132
127
 
133
128
  ### `can-grow`
134
129
 
135
- Creates an object that can be resized using a `scale` `transform`. Clicking the object will grow it, clicking with <kbd>ALT</kbd> will shrink it. Currently, the max size is 2x the original size and the min size is 1/2 the original size.
130
+ Creates an element that can be resized using a `scale` `transform`. Clicking the element will grow it, clicking with <kbd>ALT</kbd> will shrink it. Currently, the max size is 2x the original size and the min size is 1/2 the original size.
131
+
132
+ ### `can-spin`
133
+
134
+ Creates a rotatable element using a `rotate` `transform` on the element. Dragging the element to the left or right will rotate it counter-clockwise and clockwise respectively.
136
135
 
137
136
  ### `can-post`
138
137
 
@@ -140,21 +139,9 @@ Creates an object that can be resized using a `scale` `transform`. Clicking the
140
139
 
141
140
  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
142
141
 
143
- ---
144
-
145
- To add new ones, please find contributor guidelines in [New Capabilities](#new-capabilities).
146
-
147
142
  ## Contributing
148
143
 
149
- ### New Capabilities
150
-
151
- `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.
152
-
153
- To contribute your capability, see sample PR (TODO: LINK).
154
-
155
- ---
156
-
157
- Outside of contributing new capabilities, feel free to submit any issues or PRs for bugs or improvements to the core of the library.
144
+ See [CONTRIBUTING.md](https://github.com/spencerc99/playhtml/blob/main/CONTRIBUTING.md).
158
145
 
159
146
  ## Support & Maintenance
160
147
 
@@ -1,49 +1,5 @@
1
1
  /// <reference lib="dom" />
2
- import { TagType } from "./types";
3
- type ModifierKey = "ctrlKey" | "altKey" | "shiftKey" | "metaKey";
4
- export interface ElementInitializer<T = any, U = any, V = any> {
5
- defaultData: T | ((element: HTMLElement) => T);
6
- defaultLocalData?: U | ((element: HTMLElement) => U);
7
- myDefaultAwareness?: V | ((element: HTMLElement) => V);
8
- updateElement: (data: ElementEventHandlerData<T, U, V>) => void;
9
- updateElementAwareness?: (data: ElementAwarenessEventHandlerData<T, U, V>) => void;
10
- onDrag?: (e: MouseEvent | TouchEvent, eventData: ElementEventHandlerData<T, U, V>) => void;
11
- onClick?: (e: MouseEvent, eventData: ElementEventHandlerData<T, U, V>) => void;
12
- onDragStart?: (e: MouseEvent | TouchEvent, eventData: ElementEventHandlerData<T, U, V>) => void;
13
- additionalSetup?: (eventData: ElementSetupData<T, U, V>) => void;
14
- resetShortcut?: ModifierKey;
15
- debounceMs?: number;
16
- }
17
- export interface ElementData<T = any, U = any, V = any> extends ElementInitializer<T> {
18
- data?: T;
19
- localData?: U;
20
- awareness?: V;
21
- element: HTMLElement;
22
- onChange: (data: T) => void;
23
- onAwarenessChange: (data: V) => void;
24
- triggerAwarenessUpdate: () => void;
25
- }
26
- interface ElementEventHandlerData<T = any, U = any, V = any> {
27
- data: T;
28
- localData: U;
29
- awareness: V[];
30
- element: HTMLElement;
31
- setData: (data: T) => void;
32
- setLocalData: (data: U) => void;
33
- setLocalAwareness: (data: V) => void;
34
- }
35
- interface ElementAwarenessEventHandlerData<T = any, U = any, V = any> extends ElementEventHandlerData<T, U, V> {
36
- myAwareness?: V;
37
- }
38
- interface ElementSetupData<T = any, U = any, V = any> {
39
- getData: () => T;
40
- getLocalData: () => U;
41
- getAwareness: () => V[];
42
- getElement: () => HTMLElement;
43
- setData: (data: T) => void;
44
- setLocalData: (data: U) => void;
45
- setLocalAwareness: (data: V) => void;
46
- }
2
+ import { ElementAwarenessEventHandlerData, ElementData, ElementEventHandlerData, ElementInitializer, ElementSetupData, ModifierKey, TagType } from "./types";
47
3
  export declare const TagTypeToElement: Record<Exclude<TagType, "can-play">, ElementInitializer>;
48
4
  export declare class ElementHandler<T = any, U = any, V = any> {
49
5
  defaultData: T;
@@ -85,4 +41,3 @@ export declare class ElementHandler<T = any, U = any, V = any> {
85
41
  */
86
42
  reset(): void;
87
43
  }
88
- export {};
package/dist/main.d.ts CHANGED
@@ -1,11 +1,23 @@
1
1
  /// <reference lib="dom" />
2
+ import { TagType } from "./types";
2
3
  import * as Y from "yjs";
3
4
  import { ElementHandler } from "./elements";
4
- export declare const globalData: Y.Map<Y.Map<any>>;
5
5
  export declare function getElementFromId(id: string): HTMLElement | null;
6
- export declare const elementHandlers: Map<string, Map<string, ElementHandler<any, any, any>>>;
7
6
  /**
8
7
  * Sets up any playhtml elements that are currently on the page.
9
- * Can be repeatedly called to set up new elements without affecting existing ones.
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`.
10
11
  */
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;
Binary file
Binary file
Binary file