xote 1.0.2 → 1.0.3

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.
@@ -1,36 +0,0 @@
1
- module IntSet = Belt.Set.Int
2
- module IntMap = Belt.Map.Int
3
-
4
- module Id = Xote__Id
5
- module Observer = Xote__Observer
6
- module Signal = Xote__Signal
7
- module Core = Xote__Core
8
-
9
- type disposer = {dispose: unit => unit}
10
-
11
- let run = (fn: unit => unit): disposer => {
12
- let id = Id.make()
13
- let rec o: Observer.t = {
14
- id,
15
- kind: #Effect,
16
- run: () => fn(),
17
- deps: IntSet.empty,
18
- }
19
- Core.observers := IntMap.set(Core.observers.contents, id, o)
20
- /* initial run */
21
- Core.clearDeps(o)
22
- Core.currentObserverId := Some(id)
23
- o.run()
24
- Core.currentObserverId := None
25
-
26
- let dispose = () => {
27
- switch IntMap.get(Core.observers.contents, id) {
28
- | None => ()
29
- | Some(o) => {
30
- Core.clearDeps(o)
31
- Core.observers := IntMap.remove(Core.observers.contents, id)
32
- }
33
- }
34
- }
35
- {dispose: dispose}
36
- }
@@ -1,57 +0,0 @@
1
- // Generated by ReScript, PLEASE EDIT WITH CARE
2
-
3
- import * as Xote__Id from "./Xote__Id.res.mjs";
4
- import * as Xote__Core from "./Xote__Core.res.mjs";
5
- import * as Belt_MapInt from "rescript/lib/es6/belt_MapInt.js";
6
-
7
- function run(fn) {
8
- var id = Xote__Id.make();
9
- var o = {
10
- id: id,
11
- kind: "Effect",
12
- run: (function () {
13
- fn();
14
- }),
15
- deps: undefined
16
- };
17
- Xote__Core.observers.contents = Belt_MapInt.set(Xote__Core.observers.contents, id, o);
18
- Xote__Core.clearDeps(o);
19
- Xote__Core.currentObserverId.contents = id;
20
- o.run();
21
- Xote__Core.currentObserverId.contents = undefined;
22
- var dispose = function () {
23
- var o = Belt_MapInt.get(Xote__Core.observers.contents, id);
24
- if (o !== undefined) {
25
- Xote__Core.clearDeps(o);
26
- Xote__Core.observers.contents = Belt_MapInt.remove(Xote__Core.observers.contents, id);
27
- return ;
28
- }
29
-
30
- };
31
- return {
32
- dispose: dispose
33
- };
34
- }
35
-
36
- var IntSet;
37
-
38
- var IntMap;
39
-
40
- var Id;
41
-
42
- var Observer;
43
-
44
- var Signal;
45
-
46
- var Core;
47
-
48
- export {
49
- IntSet ,
50
- IntMap ,
51
- Id ,
52
- Observer ,
53
- Signal ,
54
- Core ,
55
- run ,
56
- }
57
- /* No side effect */
@@ -1,266 +0,0 @@
1
- open Xote
2
-
3
- type todo = {
4
- id: int,
5
- text: string,
6
- completed: bool,
7
- }
8
-
9
- let todos = Signal.make([])
10
- let nextId = ref(0)
11
- let inputValue = Signal.make("")
12
- let darkMode = Signal.make(false)
13
-
14
- // Computed values derived from todos
15
- let completedCount = Computed.make(() => {
16
- Signal.get(todos)->Array.filter(todo => todo.completed)->Array.length
17
- })
18
-
19
- let activeCount = Computed.make(() => {
20
- Signal.get(todos)->Array.filter(todo => !todo.completed)->Array.length
21
- })
22
-
23
- let totalCount = Computed.make(() => {
24
- Signal.get(todos)->Array.length
25
- })
26
-
27
- let addTodo = (text: string) => {
28
- if String.trim(text) != "" {
29
- Signal.update(todos, list => {
30
- Array.concat(list, [{id: nextId.contents, text, completed: false}])
31
- })
32
- nextId := nextId.contents + 1
33
- Signal.set(inputValue, "")
34
- }
35
- }
36
-
37
- let toggleTodo = (id: int) => {
38
- Signal.update(todos, list => {
39
- Array.map(list, todo => todo.id == id ? {...todo, completed: !todo.completed} : todo)
40
- })
41
- }
42
-
43
- @val @scope("document") external querySelector: string => Nullable.t<Dom.element> = "querySelector"
44
- @get external target: Dom.event => Dom.element = "target"
45
- @get external value: Dom.element => string = "value"
46
- @set external setValue: (Dom.element, string) => unit = "value"
47
- @get external key: Dom.event => string = "key"
48
-
49
- let clearInput = () => {
50
- switch querySelector(".todo-input")->Nullable.toOption {
51
- | Some(input) => setValue(input, "")
52
- | None => ()
53
- }
54
- }
55
-
56
- let handleInput = (evt: Dom.event) => {
57
- let newValue = evt->target->value
58
- Signal.set(inputValue, newValue)
59
- }
60
-
61
- let getInputValue = () => {
62
- switch querySelector(".todo-input")->Nullable.toOption {
63
- | Some(input) => input->value
64
- | None => ""
65
- }
66
- }
67
-
68
- let handleKeyDown = (evt: Dom.event) => {
69
- if evt->key == "Enter" {
70
- addTodo(getInputValue())
71
- clearInput()
72
- }
73
- }
74
-
75
- let handleAddClick = (_evt: Dom.event) => {
76
- addTodo(getInputValue())
77
- clearInput()
78
- }
79
-
80
- let toggleTheme = (_evt: Dom.event) => {
81
- Signal.update(darkMode, mode => !mode)
82
- }
83
-
84
- // Effect to sync dark mode with HTML class
85
- let _ = Effect.run(() => {
86
- let isDark = Signal.get(darkMode)
87
- if isDark {
88
- %raw(`document.documentElement.classList.add('dark')`)
89
- } else {
90
- %raw(`document.documentElement.classList.remove('dark')`)
91
- }
92
- })
93
-
94
- let todoItem = (todo: todo) => {
95
- let checkboxAttrs = todo.completed
96
- ? [("type", "checkbox"), ("checked", "checked"), ("class", "w-5 h-5 cursor-pointer")]
97
- : [("type", "checkbox"), ("class", "w-5 h-5 cursor-pointer")]
98
-
99
- Component.li(
100
- ~attrs=[
101
- (
102
- "class",
103
- "flex items-center gap-3 p-3 bg-white dark:bg-gray-800 rounded-lg border border-gray-200 dark:border-gray-700 mb-2 " ++
104
- (todo.completed ? "completed" : ""),
105
- ),
106
- ],
107
- ~children=[
108
- Component.input(~attrs=checkboxAttrs, ~events=[("change", _ => toggleTodo(todo.id))], ()),
109
- Component.span(
110
- ~attrs=[("class", "flex-1 text-gray-900 dark:text-gray-100")],
111
- ~children=[Component.text(todo.text)],
112
- (),
113
- ),
114
- ],
115
- (),
116
- )
117
- }
118
-
119
- let inputElement = Component.input(
120
- ~attrs=[
121
- ("type", "text"),
122
- ("placeholder", "What needs to be done?"),
123
- ("class", "todo-input flex-1 px-4 py-2 border border-gray-300 dark:border-gray-600 rounded-lg bg-white dark:bg-gray-800 text-gray-900 dark:text-gray-100 focus:outline-none focus:ring-2 focus:ring-blue-500"),
124
- ],
125
- ~events=[("input", handleInput), ("keydown", handleKeyDown)],
126
- (),
127
- )
128
-
129
- let app = Component.div(
130
- ~attrs=[("class", "max-w-2xl mx-auto p-6 space-y-6")],
131
- ~children=[
132
- Component.div(
133
- ~attrs=[("class", "bg-white dark:bg-gray-800 rounded-xl shadow-lg p-8")],
134
- ~children=[
135
- Component.div(
136
- ~attrs=[("class", "flex items-center justify-between mb-6")],
137
- ~children=[
138
- Component.h1(
139
- ~attrs=[("class", "text-3xl font-bold text-gray-900 dark:text-white")],
140
- ~children=[Component.text("Todo List")],
141
- (),
142
- ),
143
- Component.button(
144
- ~attrs=[
145
- (
146
- "class",
147
- "px-4 py-2 rounded-lg bg-gray-200 dark:bg-gray-700 text-gray-800 dark:text-gray-200 hover:bg-gray-300 dark:hover:bg-gray-600 transition-colors",
148
- ),
149
- ],
150
- ~events=[("click", toggleTheme)],
151
- ~children=[
152
- Component.textSignal(
153
- Computed.make(() => Signal.get(darkMode) ? "☀️ Light" : "🌙 Dark")
154
- ),
155
- ],
156
- (),
157
- ),
158
- ],
159
- (),
160
- ),
161
- Component.div(
162
- ~attrs=[
163
- (
164
- "class",
165
- "grid grid-cols-3 gap-4 mb-6 p-4 bg-gray-50 dark:bg-gray-900 rounded-lg border border-gray-200 dark:border-gray-700",
166
- ),
167
- ],
168
- ~children=[
169
- Component.div(
170
- ~attrs=[("class", "flex flex-col items-center")],
171
- ~children=[
172
- Component.span(
173
- ~attrs=[
174
- ("class", "text-xs text-gray-600 dark:text-gray-400 uppercase tracking-wide mb-1"),
175
- ],
176
- ~children=[Component.text("Total")],
177
- (),
178
- ),
179
- Component.span(
180
- ~attrs=[("class", "text-2xl font-bold text-gray-900 dark:text-white")],
181
- ~children=[
182
- Component.textSignal(Computed.make(() => Int.toString(Signal.get(totalCount)))),
183
- ],
184
- (),
185
- ),
186
- ],
187
- (),
188
- ),
189
- Component.div(
190
- ~attrs=[("class", "flex flex-col items-center")],
191
- ~children=[
192
- Component.span(
193
- ~attrs=[
194
- ("class", "text-xs text-gray-600 dark:text-gray-400 uppercase tracking-wide mb-1"),
195
- ],
196
- ~children=[Component.text("Active")],
197
- (),
198
- ),
199
- Component.span(
200
- ~attrs=[("class", "text-2xl font-bold text-blue-600 dark:text-blue-400")],
201
- ~children=[
202
- Component.textSignal(
203
- Computed.make(() => Int.toString(Signal.get(activeCount)))
204
- ),
205
- ],
206
- (),
207
- ),
208
- ],
209
- (),
210
- ),
211
- Component.div(
212
- ~attrs=[("class", "flex flex-col items-center")],
213
- ~children=[
214
- Component.span(
215
- ~attrs=[
216
- ("class", "text-xs text-gray-600 dark:text-gray-400 uppercase tracking-wide mb-1"),
217
- ],
218
- ~children=[Component.text("Completed")],
219
- (),
220
- ),
221
- Component.span(
222
- ~attrs=[("class", "text-2xl font-bold text-green-600 dark:text-green-400")],
223
- ~children=[
224
- Component.textSignal(
225
- Computed.make(() => Int.toString(Signal.get(completedCount)))
226
- ),
227
- ],
228
- (),
229
- ),
230
- ],
231
- (),
232
- ),
233
- ],
234
- (),
235
- ),
236
- Component.div(
237
- ~attrs=[("class", "flex gap-2 mb-6")],
238
- ~children=[
239
- inputElement,
240
- Component.button(
241
- ~attrs=[
242
- (
243
- "class",
244
- "px-6 py-2 bg-blue-600 hover:bg-blue-700 text-white rounded-lg font-medium transition-colors focus:outline-none focus:ring-2 focus:ring-blue-500",
245
- ),
246
- ],
247
- ~events=[("click", handleAddClick)],
248
- ~children=[Component.text("Add")],
249
- (),
250
- ),
251
- ],
252
- (),
253
- ),
254
- Component.ul(
255
- ~attrs=[("class", "todo-list space-y-2")],
256
- ~children=[Component.list(todos, todoItem)],
257
- (),
258
- ),
259
- ],
260
- (),
261
- ),
262
- ],
263
- (),
264
- )
265
-
266
- Component.mountById(app, "app")
@@ -1,303 +0,0 @@
1
- // Generated by ReScript, PLEASE EDIT WITH CARE
2
-
3
- import * as Xote__Effect from "./Xote__Effect.res.mjs";
4
- import * as Xote__Signal from "./Xote__Signal.res.mjs";
5
- import * as Xote__Computed from "./Xote__Computed.res.mjs";
6
- import * as Xote__Component from "./Xote__Component.res.mjs";
7
-
8
- var todos = Xote__Signal.make([]);
9
-
10
- var nextId = {
11
- contents: 0
12
- };
13
-
14
- var inputValue = Xote__Signal.make("");
15
-
16
- var darkMode = Xote__Signal.make(false);
17
-
18
- var completedCount = Xote__Computed.make(function () {
19
- return Xote__Signal.get(todos).filter(function (todo) {
20
- return todo.completed;
21
- }).length;
22
- });
23
-
24
- var activeCount = Xote__Computed.make(function () {
25
- return Xote__Signal.get(todos).filter(function (todo) {
26
- return !todo.completed;
27
- }).length;
28
- });
29
-
30
- var totalCount = Xote__Computed.make(function () {
31
- return Xote__Signal.get(todos).length;
32
- });
33
-
34
- function addTodo(text) {
35
- if (text.trim() !== "") {
36
- Xote__Signal.update(todos, (function (list) {
37
- return list.concat([{
38
- id: nextId.contents,
39
- text: text,
40
- completed: false
41
- }]);
42
- }));
43
- nextId.contents = nextId.contents + 1 | 0;
44
- return Xote__Signal.set(inputValue, "");
45
- }
46
-
47
- }
48
-
49
- function toggleTodo(id) {
50
- Xote__Signal.update(todos, (function (list) {
51
- return list.map(function (todo) {
52
- if (todo.id === id) {
53
- return {
54
- id: todo.id,
55
- text: todo.text,
56
- completed: !todo.completed
57
- };
58
- } else {
59
- return todo;
60
- }
61
- });
62
- }));
63
- }
64
-
65
- function clearInput() {
66
- var input = document.querySelector(".todo-input");
67
- if (!(input == null)) {
68
- input.value = "";
69
- return ;
70
- }
71
-
72
- }
73
-
74
- function handleInput(evt) {
75
- var newValue = evt.target.value;
76
- Xote__Signal.set(inputValue, newValue);
77
- }
78
-
79
- function getInputValue() {
80
- var input = document.querySelector(".todo-input");
81
- if (input == null) {
82
- return "";
83
- } else {
84
- return input.value;
85
- }
86
- }
87
-
88
- function handleKeyDown(evt) {
89
- if (evt.key === "Enter") {
90
- addTodo(getInputValue());
91
- return clearInput();
92
- }
93
-
94
- }
95
-
96
- function handleAddClick(_evt) {
97
- addTodo(getInputValue());
98
- clearInput();
99
- }
100
-
101
- function toggleTheme(_evt) {
102
- Xote__Signal.update(darkMode, (function (mode) {
103
- return !mode;
104
- }));
105
- }
106
-
107
- Xote__Effect.run(function () {
108
- var isDark = Xote__Signal.get(darkMode);
109
- if (isDark) {
110
- return (document.documentElement.classList.add('dark'));
111
- } else {
112
- return (document.documentElement.classList.remove('dark'));
113
- }
114
- });
115
-
116
- function todoItem(todo) {
117
- var checkboxAttrs = todo.completed ? [
118
- [
119
- "type",
120
- "checkbox"
121
- ],
122
- [
123
- "checked",
124
- "checked"
125
- ],
126
- [
127
- "class",
128
- "w-5 h-5 cursor-pointer"
129
- ]
130
- ] : [
131
- [
132
- "type",
133
- "checkbox"
134
- ],
135
- [
136
- "class",
137
- "w-5 h-5 cursor-pointer"
138
- ]
139
- ];
140
- return Xote__Component.li([[
141
- "class",
142
- "flex items-center gap-3 p-3 bg-white dark:bg-gray-800 rounded-lg border border-gray-200 dark:border-gray-700 mb-2 " + (
143
- todo.completed ? "completed" : ""
144
- )
145
- ]], undefined, [
146
- Xote__Component.input(checkboxAttrs, [[
147
- "change",
148
- (function (param) {
149
- toggleTodo(todo.id);
150
- })
151
- ]], undefined),
152
- Xote__Component.span([[
153
- "class",
154
- "flex-1 text-gray-900 dark:text-gray-100"
155
- ]], undefined, [Xote__Component.text(todo.text)], undefined)
156
- ], undefined);
157
- }
158
-
159
- var inputElement = Xote__Component.input([
160
- [
161
- "type",
162
- "text"
163
- ],
164
- [
165
- "placeholder",
166
- "What needs to be done?"
167
- ],
168
- [
169
- "class",
170
- "todo-input flex-1 px-4 py-2 border border-gray-300 dark:border-gray-600 rounded-lg bg-white dark:bg-gray-800 text-gray-900 dark:text-gray-100 focus:outline-none focus:ring-2 focus:ring-blue-500"
171
- ]
172
- ], [
173
- [
174
- "input",
175
- handleInput
176
- ],
177
- [
178
- "keydown",
179
- handleKeyDown
180
- ]
181
- ], undefined);
182
-
183
- var app = Xote__Component.div([[
184
- "class",
185
- "max-w-2xl mx-auto p-6 space-y-6"
186
- ]], undefined, [Xote__Component.div([[
187
- "class",
188
- "bg-white dark:bg-gray-800 rounded-xl shadow-lg p-8"
189
- ]], undefined, [
190
- Xote__Component.div([[
191
- "class",
192
- "flex items-center justify-between mb-6"
193
- ]], undefined, [
194
- Xote__Component.h1([[
195
- "class",
196
- "text-3xl font-bold text-gray-900 dark:text-white"
197
- ]], undefined, [Xote__Component.text("Todo List")], undefined),
198
- Xote__Component.button([[
199
- "class",
200
- "px-4 py-2 rounded-lg bg-gray-200 dark:bg-gray-700 text-gray-800 dark:text-gray-200 hover:bg-gray-300 dark:hover:bg-gray-600 transition-colors"
201
- ]], [[
202
- "click",
203
- toggleTheme
204
- ]], [Xote__Component.textSignal(Xote__Computed.make(function () {
205
- if (Xote__Signal.get(darkMode)) {
206
- return "☀️ Light";
207
- } else {
208
- return "🌙 Dark";
209
- }
210
- }))], undefined)
211
- ], undefined),
212
- Xote__Component.div([[
213
- "class",
214
- "grid grid-cols-3 gap-4 mb-6 p-4 bg-gray-50 dark:bg-gray-900 rounded-lg border border-gray-200 dark:border-gray-700"
215
- ]], undefined, [
216
- Xote__Component.div([[
217
- "class",
218
- "flex flex-col items-center"
219
- ]], undefined, [
220
- Xote__Component.span([[
221
- "class",
222
- "text-xs text-gray-600 dark:text-gray-400 uppercase tracking-wide mb-1"
223
- ]], undefined, [Xote__Component.text("Total")], undefined),
224
- Xote__Component.span([[
225
- "class",
226
- "text-2xl font-bold text-gray-900 dark:text-white"
227
- ]], undefined, [Xote__Component.textSignal(Xote__Computed.make(function () {
228
- return Xote__Signal.get(totalCount).toString();
229
- }))], undefined)
230
- ], undefined),
231
- Xote__Component.div([[
232
- "class",
233
- "flex flex-col items-center"
234
- ]], undefined, [
235
- Xote__Component.span([[
236
- "class",
237
- "text-xs text-gray-600 dark:text-gray-400 uppercase tracking-wide mb-1"
238
- ]], undefined, [Xote__Component.text("Active")], undefined),
239
- Xote__Component.span([[
240
- "class",
241
- "text-2xl font-bold text-blue-600 dark:text-blue-400"
242
- ]], undefined, [Xote__Component.textSignal(Xote__Computed.make(function () {
243
- return Xote__Signal.get(activeCount).toString();
244
- }))], undefined)
245
- ], undefined),
246
- Xote__Component.div([[
247
- "class",
248
- "flex flex-col items-center"
249
- ]], undefined, [
250
- Xote__Component.span([[
251
- "class",
252
- "text-xs text-gray-600 dark:text-gray-400 uppercase tracking-wide mb-1"
253
- ]], undefined, [Xote__Component.text("Completed")], undefined),
254
- Xote__Component.span([[
255
- "class",
256
- "text-2xl font-bold text-green-600 dark:text-green-400"
257
- ]], undefined, [Xote__Component.textSignal(Xote__Computed.make(function () {
258
- return Xote__Signal.get(completedCount).toString();
259
- }))], undefined)
260
- ], undefined)
261
- ], undefined),
262
- Xote__Component.div([[
263
- "class",
264
- "flex gap-2 mb-6"
265
- ]], undefined, [
266
- inputElement,
267
- Xote__Component.button([[
268
- "class",
269
- "px-6 py-2 bg-blue-600 hover:bg-blue-700 text-white rounded-lg font-medium transition-colors focus:outline-none focus:ring-2 focus:ring-blue-500"
270
- ]], [[
271
- "click",
272
- handleAddClick
273
- ]], [Xote__Component.text("Add")], undefined)
274
- ], undefined),
275
- Xote__Component.ul([[
276
- "class",
277
- "todo-list space-y-2"
278
- ]], undefined, [Xote__Component.list(todos, todoItem)], undefined)
279
- ], undefined)], undefined);
280
-
281
- Xote__Component.mountById(app, "app");
282
-
283
- export {
284
- todos ,
285
- nextId ,
286
- inputValue ,
287
- darkMode ,
288
- completedCount ,
289
- activeCount ,
290
- totalCount ,
291
- addTodo ,
292
- toggleTodo ,
293
- clearInput ,
294
- handleInput ,
295
- getInputValue ,
296
- handleKeyDown ,
297
- handleAddClick ,
298
- toggleTheme ,
299
- todoItem ,
300
- inputElement ,
301
- app ,
302
- }
303
- /* todos Not a pure module */
package/src/Xote__Id.res DELETED
@@ -1,5 +0,0 @@
1
- let next = ref(0)
2
- let make = () => {
3
- next := next.contents + 1
4
- next.contents
5
- }
@@ -1,17 +0,0 @@
1
- // Generated by ReScript, PLEASE EDIT WITH CARE
2
-
3
-
4
- var next = {
5
- contents: 0
6
- };
7
-
8
- function make() {
9
- next.contents = next.contents + 1 | 0;
10
- return next.contents;
11
- }
12
-
13
- export {
14
- next ,
15
- make ,
16
- }
17
- /* No side effect */
@@ -1,12 +0,0 @@
1
- module IntSet = Belt.Set.Int
2
- module IntMap = Belt.Map.Int
3
-
4
- type kind = [#Effect | #Computed(int)] /* holds the signal id it writes to */
5
-
6
- type t = {
7
- id: int,
8
- kind: kind,
9
- run: unit => unit,
10
- /* current dependency set (signal ids) */
11
- mutable deps: IntSet.t,
12
- }