playhtml 1.1.0 → 1.3.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 ADDED
@@ -0,0 +1,11 @@
1
+ # Change Log
2
+
3
+ The format is based on Keep a Changelog and this project adheres to Semantic Versioning.
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
+
9
+ ## 1.2.0 - 2023-08-03
10
+
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.
@@ -42,6 +40,14 @@ If you have dynamic elements that are hydrated after the initial load, you can c
42
40
  </script>
43
41
  ```
44
42
 
43
+ To create your own custom element, refer to the [can-play](#can-play) section.
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
+ ## Examples
48
+
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
+
45
51
  ## Capabilities
46
52
 
47
53
  A full list can be found in `types.ts` under `TagType`.
@@ -97,6 +103,8 @@ https://github.com/spencerc99/playhtml/assets/14796580/fae669b1-b3e2-404e-bd7a-3
97
103
 
98
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`.
99
105
 
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.
107
+
100
108
  ## Plug-and-play Capabilities
101
109
 
102
110
  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.
@@ -107,26 +115,30 @@ We welcome any contributions if you have custom `can-play` elements that you wou
107
115
 
108
116
  https://github.com/spencerc99/playhtml/assets/14796580/9c2b9bf6-142c-41e2-8c8f-93a3b121a73e
109
117
 
110
- Creates a movable object using 2D `translate` on the element. Dragging the element around will move it
118
+ Creates a movable element using 2D `translate` on the element. Dragging the element around will move it
111
119
 
112
120
  **troubleshooting**
113
121
 
114
122
  - This currently doesn't work on `inline` display elements.
115
123
  - This currently doesn't work on touch screens.
116
124
 
117
- ### `can-spin`
118
-
119
- 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.
120
-
121
125
  ### `can-toggle`
122
126
 
123
127
  <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>
124
128
 
125
- Creates an object that can be switched on and off. Clicking the element will toggle the `clicked` class on the element.
129
+ Creates an element that can be switched on and off. Clicking the element will toggle the `clicked` class on the element.
130
+
131
+ ### `can-duplicate`
132
+
133
+ 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).
126
134
 
127
135
  ### `can-grow`
128
136
 
129
- 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.
137
+ 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.
138
+
139
+ ### `can-spin`
140
+
141
+ 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.
130
142
 
131
143
  ### `can-post`
132
144
 
@@ -1,48 +1,65 @@
1
1
  /// <reference lib="dom" />
2
2
  import { TagType } from "./types";
3
3
  type ModifierKey = "ctrlKey" | "altKey" | "shiftKey" | "metaKey";
4
- export interface ElementInitializer<T = any, U = any> {
4
+ export interface ElementInitializer<T = any, U = any, V = any> {
5
5
  defaultData: T | ((element: HTMLElement) => T);
6
6
  defaultLocalData?: U | ((element: HTMLElement) => U);
7
- updateElement: (data: ElementEventHandlerData<T, U>) => void;
8
- onDrag?: (e: MouseEvent | TouchEvent, eventData: ElementEventHandlerData<T, U>) => void;
9
- onClick?: (e: MouseEvent, eventData: ElementEventHandlerData<T, U>) => void;
10
- onDragStart?: (e: MouseEvent | TouchEvent, eventData: ElementEventHandlerData<T, U>) => void;
11
- additionalSetup?: (eventData: ElementSetupData<T, U>) => void;
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;
12
14
  resetShortcut?: ModifierKey;
13
15
  debounceMs?: number;
14
16
  }
15
- export interface ElementData<T = any, U = any> extends ElementInitializer<T> {
17
+ export interface ElementData<T = any, U = any, V = any> extends ElementInitializer<T> {
16
18
  data?: T;
17
19
  localData?: U;
20
+ awareness?: V;
18
21
  element: HTMLElement;
19
22
  onChange: (data: T) => void;
23
+ onAwarenessChange: (data: V) => void;
24
+ triggerAwarenessUpdate: () => void;
20
25
  }
21
- interface ElementEventHandlerData<T = any, U = any> {
26
+ interface ElementEventHandlerData<T = any, U = any, V = any> {
22
27
  data: T;
23
28
  localData: U;
29
+ awareness: V[];
24
30
  element: HTMLElement;
25
31
  setData: (data: T) => void;
26
32
  setLocalData: (data: U) => void;
33
+ setLocalAwareness: (data: V) => void;
27
34
  }
28
- interface ElementSetupData<T = any, U = any> {
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> {
29
39
  getData: () => T;
30
40
  getLocalData: () => U;
41
+ getAwareness: () => V[];
31
42
  getElement: () => HTMLElement;
32
43
  setData: (data: T) => void;
33
44
  setLocalData: (data: U) => void;
45
+ setLocalAwareness: (data: V) => void;
34
46
  }
35
47
  export declare const TagTypeToElement: Record<Exclude<TagType, "can-play">, ElementInitializer>;
36
- export declare class ElementHandler<T = any, U = any> {
48
+ export declare class ElementHandler<T = any, U = any, V = any> {
37
49
  defaultData: T;
38
50
  localData: U;
51
+ awareness: V[];
52
+ selfAwareness?: V;
39
53
  element: HTMLElement;
40
54
  _data: T;
41
55
  onChange: (data: T) => void;
56
+ onAwarenessChange: (data: V) => void;
42
57
  debouncedOnChange: (data: T) => void;
43
58
  resetShortcut?: ModifierKey;
44
- updateElement: (data: ElementEventHandlerData<T, U>) => void;
45
- constructor({ element, onChange, defaultData, defaultLocalData, data, updateElement, onClick, onDrag, onDragStart, additionalSetup, resetShortcut, debounceMs, }: ElementData<T>);
59
+ updateElement: (data: ElementEventHandlerData<T, U, V>) => void;
60
+ updateElementAwareness?: (data: ElementAwarenessEventHandlerData<T, U, V>) => void;
61
+ triggerAwarenessUpdate?: () => void;
62
+ constructor({ element, onChange, onAwarenessChange, defaultData, defaultLocalData, myDefaultAwareness, data, awareness: awarenessData, updateElement, updateElementAwareness, onClick, onDrag, onDragStart, additionalSetup, resetShortcut, debounceMs, triggerAwarenessUpdate, }: ElementData<T>);
46
63
  get data(): T;
47
64
  setLocalData(localData: U): void;
48
65
  /**
@@ -53,12 +70,15 @@ export declare class ElementHandler<T = any, U = any> {
53
70
  * (e.g. calling `updateElement` and `onChange`)
54
71
  */
55
72
  set __data(data: T);
56
- getEventHandlerData(): ElementEventHandlerData<T, U>;
73
+ set __awareness(data: V[]);
74
+ getEventHandlerData(): ElementEventHandlerData<T, U, V>;
75
+ getAwarenessEventHandlerData(): ElementAwarenessEventHandlerData<T, U, V>;
57
76
  getSetupData(): ElementSetupData<T, U>;
58
77
  /**
59
78
  * Public-use setter for data that makes the change to all clients.
60
79
  */
61
80
  setData(data: T): void;
81
+ setLocalAwareness(data: V): void;
62
82
  setDataDebounced(data: T): void;
63
83
  /**
64
84
  * Resets the element to its default state.
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>>>;
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