braid-text 0.3.25 → 0.3.26

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
@@ -188,7 +188,7 @@ This will render each peer's cursor and selection with colored highlights. Just
188
188
 
189
189
  ```html
190
190
  <!-- 1. Include these additional script tags -->
191
- <script src="https://unpkg.com/braid-text@~0.3/client/cursor-highlights.js"></script>
191
+ <script src="https://unpkg.com/braid-text@~0.3/client/textarea-highlights.js"></script>
192
192
  <script src="https://unpkg.com/braid-text@~0.3/client/cursor-sync.js"></script>
193
193
 
194
194
  <!-- 2. Add two lines to your simpleton_client() call -->
@@ -229,6 +229,13 @@ async function cursor_client(url, { peer, get_text, on_change, headers: custom_h
229
229
  last_sent = key
230
230
  last_ranges = ranges
231
231
 
232
+ // Report local cursor immediately for rendering
233
+ if (on_change) {
234
+ var update = {}
235
+ update[peer] = last_ranges
236
+ on_change(update)
237
+ }
238
+
232
239
  if (!online) return
233
240
 
234
241
  // Debounce 50ms
@@ -236,29 +243,34 @@ async function cursor_client(url, { peer, get_text, on_change, headers: custom_h
236
243
  send_timer = setTimeout(function() { do_put(ranges) }, 50)
237
244
  },
238
245
 
239
- // Transform all stored remote cursor positions through text edits.
246
+ // Transform all cursor positions (remote and local) through text edits.
240
247
  // patches: [{ range: [start, end], content: string }] in JS string indices.
241
248
  changed: function(patches) {
242
- var any_changed = false
243
- for (var id of Object.keys(selections)) {
244
- selections[id] = selections[id].map(function(sel) {
245
- var from = sel.from
246
- var to = sel.to
249
+ function transform_ranges(ranges) {
250
+ return ranges.map(function(sel) {
251
+ var from = sel.from, to = sel.to
247
252
  for (var p of patches) {
248
- var del_len = p.range[1] - p.range[0]
249
- var ins_len = p.content.length
250
- from = transform_pos(from, p.range[0], del_len, ins_len)
251
- to = transform_pos(to, p.range[0], del_len, ins_len)
253
+ var dl = p.range[1] - p.range[0], il = p.content.length
254
+ from = transform_pos(from, p.range[0], dl, il)
255
+ to = transform_pos(to, p.range[0], dl, il)
252
256
  }
253
257
  return { from, to }
254
258
  })
255
- any_changed = true
256
259
  }
257
- if (any_changed && on_change) {
258
- // Report all current selections
260
+
261
+ for (var id of Object.keys(selections))
262
+ selections[id] = transform_ranges(selections[id])
263
+
264
+ if (last_ranges)
265
+ last_ranges = transform_ranges(last_ranges)
266
+
267
+ // Report all cursors including local
268
+ if (on_change) {
259
269
  var all = {}
260
270
  for (var id of Object.keys(selections))
261
271
  all[id] = selections[id]
272
+ if (last_ranges)
273
+ all[peer] = last_ranges
262
274
  on_change(all)
263
275
  }
264
276
  },
@@ -278,3 +290,120 @@ async function cursor_client(url, { peer, get_text, on_change, headers: custom_h
278
290
  },
279
291
  }
280
292
  }
293
+
294
+ // --- Color helpers ---
295
+
296
+ var _cursor_colors = ["#e06c75", "#61afef", "#98c379", "#c678dd", "#e5c07b", "#56b6c2"]
297
+
298
+ function peer_color(peer_id) {
299
+ var hash = 0
300
+ for (var i = 0; i < peer_id.length; i++)
301
+ hash = ((hash << 5) - hash + peer_id.charCodeAt(i)) | 0
302
+ return _cursor_colors[Math.abs(hash) % _cursor_colors.length]
303
+ }
304
+
305
+ function peer_bg_color(peer_id) {
306
+ var c = peer_color(peer_id)
307
+ var r = parseInt(c.slice(1, 3), 16)
308
+ var g = parseInt(c.slice(3, 5), 16)
309
+ var b = parseInt(c.slice(5, 7), 16)
310
+ var dark = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches
311
+ return `rgba(${r}, ${g}, ${b}, ${dark ? 0.4 : 0.25})`
312
+ }
313
+
314
+ // --- High-level wrapper: textarea + cursor sync ---
315
+ //
316
+ // Requires textarea_highlights (from textarea-highlights.js)
317
+ //
318
+ // Usage:
319
+ // var cursors = cursor_highlights(textarea, url, { headers: {...} })
320
+ // cursors.on_patches(patches) // call after applying remote patches
321
+ // cursors.on_edit(patches) // call after local edit; patches optional
322
+ // cursors.destroy()
323
+ //
324
+ function cursor_highlights(textarea, url, options) {
325
+ var peer = Math.random().toString(36).slice(2)
326
+ var hl = textarea_highlights(textarea)
327
+ var applying_remote = false
328
+ var client = null
329
+ var online = false
330
+ var destroyed = false
331
+
332
+ cursor_client(url, {
333
+ peer,
334
+ headers: options?.headers,
335
+ get_text: () => textarea.value,
336
+ on_change: function(sels) {
337
+ for (var [id, ranges] of Object.entries(sels)) {
338
+ // Skip own cursor when textarea is focused (browser draws it)
339
+ if (id === peer && document.activeElement === textarea) {
340
+ hl.remove(id)
341
+ continue
342
+ }
343
+ if (!ranges.length) { hl.remove(id); continue }
344
+ hl.set(id, ranges.map(r => ({
345
+ from: r.from, to: r.to,
346
+ color: r.from === r.to ? peer_color(id) : peer_bg_color(id)
347
+ })))
348
+ }
349
+ hl.render()
350
+ }
351
+ }).then(function(c) {
352
+ client = c
353
+ if (online) client.online()
354
+ if (destroyed) client.destroy()
355
+ })
356
+
357
+ function on_selectionchange() {
358
+ if (applying_remote) return
359
+ if (document.activeElement !== textarea) return
360
+ if (client) client.set(textarea.selectionStart, textarea.selectionEnd)
361
+ }
362
+ document.addEventListener('selectionchange', on_selectionchange)
363
+
364
+ // Show own cursor when blurred, hide when focused
365
+ function on_blur() {
366
+ if (client) client.set(textarea.selectionStart, textarea.selectionEnd)
367
+ }
368
+ function on_focus() {
369
+ hl.remove(peer)
370
+ hl.render()
371
+ }
372
+ textarea.addEventListener('blur', on_blur)
373
+ textarea.addEventListener('focus', on_focus)
374
+
375
+ return {
376
+ online: function() {
377
+ online = true
378
+ if (client) client.online()
379
+ },
380
+ offline: function() {
381
+ online = false
382
+ if (client) client.offline()
383
+ },
384
+
385
+ on_patches: function(patches) {
386
+ applying_remote = true
387
+ // cursor-sync transforms all cursors (remote + local) uniformly
388
+ if (client) client.changed(patches)
389
+ hl.render()
390
+ setTimeout(() => { applying_remote = false }, 0)
391
+ },
392
+
393
+ on_edit: function(patches) {
394
+ if (client) {
395
+ if (patches) client.changed(patches)
396
+ client.set(textarea.selectionStart, textarea.selectionEnd)
397
+ }
398
+ },
399
+
400
+ destroy: function() {
401
+ destroyed = true
402
+ document.removeEventListener('selectionchange', on_selectionchange)
403
+ textarea.removeEventListener('blur', on_blur)
404
+ textarea.removeEventListener('focus', on_focus)
405
+ if (client) client.destroy()
406
+ hl.destroy()
407
+ }
408
+ }
409
+ }
@@ -1,4 +1,10 @@
1
- // cursor-highlights.js — Render colored cursors and selections behind a <textarea>
1
+ // textarea-highlights.js — Render colored highlights behind a <textarea>
2
+ //
3
+ // No dependencies. Pure DOM/CSS.
4
+ //
5
+ // Renders colored ranges (selections) and zero-width points (cursors) as
6
+ // overlays behind a textarea's text. Works with any textarea regardless
7
+ // of its parent's CSS — backdrops are positioned as absolute siblings.
2
8
  //
3
9
  // Usage:
4
10
  // var hl = textarea_highlights(textarea)
@@ -211,37 +217,6 @@ function textarea_highlights(textarea) {
211
217
  }
212
218
  }
213
219
 
214
- // --- show local cursor/selection when textarea is not focused ---
215
-
216
- var local_id = '__local__'
217
-
218
- function on_blur() {
219
- var from = textarea.selectionStart
220
- var to = textarea.selectionEnd
221
- var color = getComputedStyle(textarea).caretColor
222
- if (!color || color === 'auto') color = getComputedStyle(textarea).color
223
- if (from === to) {
224
- layer_data[local_id] = [{ from, to, color: color }]
225
- } else {
226
- var match = color.match(/(\d+),\s*(\d+),\s*(\d+)/)
227
- var sel_color = match ? 'rgba(' + match[1] + ', ' + match[2] + ', ' + match[3] + ', 0.3)' : color
228
- layer_data[local_id] = [{ from, to, color: sel_color }]
229
- }
230
- do_render()
231
- }
232
-
233
- function on_focus() {
234
- delete layer_data[local_id]
235
- if (layer_divs[local_id]) {
236
- layer_divs[local_id].remove()
237
- delete layer_divs[local_id]
238
- }
239
- do_render()
240
- }
241
-
242
- textarea.addEventListener('blur', on_blur)
243
- textarea.addEventListener('focus', on_focus)
244
-
245
220
  return {
246
221
  set: function(layer_id, highlights) {
247
222
  layer_data[layer_id] = highlights
@@ -257,118 +232,15 @@ function textarea_highlights(textarea) {
257
232
 
258
233
  render: do_render,
259
234
 
260
- layers: function() {
261
- return Object.keys(layer_data)
262
- },
263
-
264
235
  destroy: function() {
265
236
  textarea.removeEventListener('scroll', sync_scroll)
266
- textarea.removeEventListener('blur', on_blur)
267
- textarea.removeEventListener('focus', on_focus)
268
237
  resize_observer.disconnect()
269
238
  for (var div of Object.values(layer_divs)) div.remove()
270
239
  layer_data = {}
271
240
  layer_divs = {}
272
- // Restore textarea styles
273
241
  textarea.style.backgroundColor = original_bg
274
242
  textarea.style.position = original_position
275
243
  textarea.style.zIndex = original_zIndex
276
244
  }
277
245
  }
278
246
  }
279
-
280
- // --- Color helpers ---
281
-
282
- var _cursor_colors = ["#e06c75", "#61afef", "#98c379", "#c678dd", "#e5c07b", "#56b6c2"]
283
-
284
- function peer_color(peer_id) {
285
- var hash = 0
286
- for (var i = 0; i < peer_id.length; i++)
287
- hash = ((hash << 5) - hash + peer_id.charCodeAt(i)) | 0
288
- return _cursor_colors[Math.abs(hash) % _cursor_colors.length]
289
- }
290
-
291
- function peer_bg_color(peer_id) {
292
- var c = peer_color(peer_id)
293
- var r = parseInt(c.slice(1, 3), 16)
294
- var g = parseInt(c.slice(3, 5), 16)
295
- var b = parseInt(c.slice(5, 7), 16)
296
- var dark = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches
297
- return `rgba(${r}, ${g}, ${b}, ${dark ? 0.4 : 0.25})`
298
- }
299
-
300
- // --- High-level wrapper ---
301
- //
302
- // Usage:
303
- // var cursors = cursor_highlights(textarea, url)
304
- // cursors.on_patches(patches) // call after applying remote patches
305
- // cursors.on_edit(patches) // call after local edit; patches optional
306
- // cursors.destroy()
307
- //
308
- function cursor_highlights(textarea, url, options) {
309
- var peer = Math.random().toString(36).slice(2)
310
- var hl = textarea_highlights(textarea)
311
- var applying_remote = false
312
- var client = null
313
- var online = false
314
- var destroyed = false
315
-
316
- cursor_client(url, {
317
- peer,
318
- headers: options?.headers,
319
- get_text: () => textarea.value,
320
- on_change: (sels) => {
321
- for (var [id, ranges] of Object.entries(sels)) {
322
- if (!ranges.length) { hl.remove(id); continue }
323
- hl.set(id, ranges.map(r => ({
324
- from: r.from, to: r.to,
325
- color: r.from === r.to ? peer_color(id) : peer_bg_color(id)
326
- })))
327
- }
328
- hl.render()
329
- }
330
- }).then(function(c) {
331
- client = c
332
- if (online) client.online()
333
- if (destroyed) client.destroy()
334
- })
335
-
336
- function on_selectionchange() {
337
- if (applying_remote) return
338
- if (document.activeElement !== textarea) return
339
- if (client) client.set(textarea.selectionStart, textarea.selectionEnd)
340
- }
341
- document.addEventListener('selectionchange', on_selectionchange)
342
-
343
- return {
344
- online: function() {
345
- online = true
346
- if (client) client.online()
347
- },
348
- offline: function() {
349
- online = false
350
- if (client) client.offline()
351
- },
352
-
353
- on_patches: function(patches) {
354
- applying_remote = true
355
- if (client) client.changed(patches)
356
- hl.render()
357
- setTimeout(() => { applying_remote = false }, 0)
358
- },
359
-
360
- on_edit: function(patches) {
361
- if (client) {
362
- if (patches) client.changed(patches)
363
- client.set(textarea.selectionStart, textarea.selectionEnd)
364
- }
365
- },
366
-
367
- destroy: function() {
368
- destroyed = true
369
- document.removeEventListener('selectionchange', on_selectionchange)
370
- if (client) client.destroy()
371
- hl.destroy()
372
- }
373
- }
374
- }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "braid-text",
3
- "version": "0.3.25",
3
+ "version": "0.3.26",
4
4
  "description": "Library for collaborative text over http using braid.",
5
5
  "author": "Braid Working Group",
6
6
  "repository": "braid-org/braid-text",
@@ -17,6 +17,7 @@
17
17
  "client/simpleton-sync.js",
18
18
  "client/cursor-sync.js",
19
19
  "client/cursor-highlights.js",
20
+ "client/textarea-highlights.js",
20
21
  "client/web-utils.js",
21
22
  "README.md",
22
23
  "client/editor.html",