@zzish/math-rich-input 0.1.54 → 0.1.55

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zzish/math-rich-input",
3
- "version": "0.1.54",
3
+ "version": "0.1.55",
4
4
  "description": "React component for user to enter rich text with embedded math equations.",
5
5
  "repository": {
6
6
  "type": "git",
@@ -23,6 +23,8 @@
23
23
  }
24
24
 
25
25
  .MathRichInput {
26
+ /* The hint below is laid over this box rather than inside its flow — see `.is-empty::before`. */
27
+ position: relative;
26
28
  font-size: 18px;
27
29
  padding: 10px;
28
30
  width: 100%;
@@ -32,13 +34,30 @@
32
34
  overflow: overlay;
33
35
  }
34
36
 
35
- .MathRichInput:empty::before {
36
- content: attr(data-placeholder);
37
+ /*
38
+ * The hint shown in a field with nothing in it.
39
+ *
40
+ * Laid over the editable, never inside it: a caret shares that box, and a pseudo-element in a
41
+ * `contenteditable` host is not drawn dependably. Placed at the field's own padding so the words start
42
+ * exactly where the first character will.
43
+ */
44
+ .MathRichInput-hintlayer {
45
+ position: relative;
46
+ width: 0;
47
+ height: 0;
48
+ flex: 0 0 auto;
49
+ }
50
+
51
+ .MathRichInput-hint {
52
+ position: absolute;
53
+ top: 10px;
54
+ left: 10px;
37
55
  color: #acb6c0;
38
56
  font-size: 15px;
39
- }
40
- .MathRichInput:empty:focus::before {
41
- content: "";
57
+ /* The field underneath owns every click. */
58
+ pointer-events: none;
59
+ user-select: none;
60
+ white-space: nowrap;
42
61
  }
43
62
 
44
63
  .MathRichInput:focus {
@@ -2726,6 +2726,24 @@ export default class MathRichInput extends React.Component {
2726
2726
  return true;
2727
2727
  }
2728
2728
 
2729
+ /**
2730
+ * Whether the field holds nothing a teacher would call content.
2731
+ *
2732
+ * NOT `:empty`, which is what the stylesheet asked and why the placeholder has never once been seen.
2733
+ * An empty field here is not an empty element: the value renders as a paragraph carrying a single
2734
+ * zero-width space, so the element always has a child and the CSS rule never matched. The hint was
2735
+ * written, translated, passed down through every field — and drawn nowhere.
2736
+ *
2737
+ * Markup is stripped rather than counted: `<p>` and `<br>` are the shape of emptiness, not content.
2738
+ * Maths and images are content even though stripping tags would leave nothing behind, so they are
2739
+ * asked about directly.
2740
+ */
2741
+ isVisuallyEmpty() {
2742
+ const value = this.props.value || "";
2743
+ if (/<(math|img)\b/i.test(value)) return false;
2744
+ return value.replace(/<[^>]*>/g, "").split(SMALL_SPACE).join("").trim().length === 0;
2745
+ }
2746
+
2729
2747
  /**
2730
2748
  * The browser's own reading of an HTML string.
2731
2749
  *
@@ -4028,6 +4046,40 @@ export default class MathRichInput extends React.Component {
4028
4046
  <>
4029
4047
  <div className={useClassName}>
4030
4048
  <div className="MathRichInput-scrollview">
4049
+ {/*
4050
+ THE HINT IS A REAL ELEMENT, and it is NOT inside the editable.
4051
+
4052
+ It was a `::before` on the editable span, shown by `:empty`. Two things were wrong with that
4053
+ and each alone was enough. `:empty` is never true here — an empty field still holds a
4054
+ paragraph with a zero-width space in it — and a pseudo-element on a `contenteditable` host is
4055
+ not something browsers draw dependably; it also sits in the same box a caret is moving
4056
+ through. The hint was written, translated and passed down through every field, and drawn in
4057
+ none of them.
4058
+
4059
+ Beside the editable and laid over it, it is ordinary content: it can be inspected, it cannot
4060
+ affect the caret, and it disappears the moment the field has something in it or the teacher
4061
+ starts typing.
4062
+ */}
4063
+ {this.props.placeholder && this.isVisuallyEmpty() && !this.state.hasFocus && (
4064
+ /*
4065
+ * CARRIED BY ITS OWN ANCHOR, a box of no size at the start of the row.
4066
+ *
4067
+ * An absolutely positioned element hangs off the nearest positioned ancestor, and which one
4068
+ * that is belongs to whoever is embedding the field: a host may take the positioning off
4069
+ * this component's own boxes to anchor something else of its own — the toolbar, say — and
4070
+ * the hint then measures from the host's outer card instead of the field, landing on top of
4071
+ * whatever the host has put to the left of it.
4072
+ *
4073
+ * A wrapper of its own settles it. Zero-sized, so it displaces nothing, and positioned, so
4074
+ * it is what the hint measures from — which puts the words where the first character goes,
4075
+ * whatever the host has done around it.
4076
+ */
4077
+ <div className="MathRichInput-hintlayer">
4078
+ <div className="MathRichInput-hint" aria-hidden="true">
4079
+ {this.props.placeholder}
4080
+ </div>
4081
+ </div>
4082
+ )}
4031
4083
  {this.state.hasFocus && (
4032
4084
  <Toolbar
4033
4085
  className="Toolbar"
package/src/standalone.js CHANGED
@@ -31,12 +31,32 @@ class MathRichInputElement extends HTMLElement {
31
31
  this.lastRenderTime = 0;
32
32
  this.renderThrottleMs = 16; // ~60fps throttle
33
33
  this.isDuringDrag = false;
34
+ /** Pending teardown, cancelled when a disconnect turns out to have been a move. */
35
+ this.teardownTimeout = null;
34
36
 
35
37
  // Setup global drag event listeners once
36
38
  this.setupDragListeners();
37
39
  }
38
40
 
39
41
  connectedCallback() {
42
+ /*
43
+ * A MOVE IS A DISCONNECT FOLLOWED BY A CONNECT, and it must not cost anything.
44
+ *
45
+ * Reordering a list moves the row, and moving a row takes this element out of the document and puts
46
+ * it back. Torn down in between, the field empties: the row loses its height, everything below it
47
+ * jumps up, and it all lands again when the field has re-rendered. In a drag that happens on every
48
+ * crossing, which reads as the list bouncing.
49
+ *
50
+ * The teardown is scheduled rather than done, so a return within the same turn simply cancels it and
51
+ * the field never notices it was moved. A real removal has no such return, and tears down as before.
52
+ */
53
+ if (this.teardownTimeout) {
54
+ clearTimeout(this.teardownTimeout);
55
+ this.teardownTimeout = null;
56
+ globalDragState.disconnectedElements.delete(this);
57
+ return;
58
+ }
59
+
40
60
  // Handle reconnection after disconnect (common in SortableJS)
41
61
  this.handleReconnection();
42
62
 
@@ -51,26 +71,45 @@ class MathRichInputElement extends HTMLElement {
51
71
  // Track this element as disconnected (for potential reconnection)
52
72
  globalDragState.disconnectedElements.add(this);
53
73
 
54
- // Clear any pending render timeouts
55
- if (this.renderTimeout) {
56
- clearTimeout(this.renderTimeout);
57
- this.renderTimeout = null;
58
- }
74
+ // Nothing is torn down yet — see `connectedCallback`. If this is a move, the element is back before
75
+ // this runs and cancels it; if it is a real removal, it runs and everything goes as it always did.
76
+ this.teardownTimeout = setTimeout(() => {
77
+ this.teardownTimeout = null;
78
+
79
+ /*
80
+ * ASK THE DOCUMENT, do not trust the timing.
81
+ *
82
+ * Cancelling on reconnect covers a move that finishes in the same turn. A framework is free to take
83
+ * the element out and put it back a turn later — which is still a move, and tearing down in between
84
+ * empties the field, collapses the row and jumps everything below it. The element itself knows
85
+ * whether it ended up back in the document, so that is what decides.
86
+ */
87
+ if (this.isConnected) return;
88
+
89
+ // Clear any pending render timeouts
90
+ if (this.renderTimeout) {
91
+ clearTimeout(this.renderTimeout);
92
+ this.renderTimeout = null;
93
+ }
59
94
 
60
- // Reset state flags
61
- this.isRendering = false;
62
- this.isDuringDrag = false;
95
+ // Reset state flags
96
+ this.isRendering = false;
97
+ this.isDuringDrag = false;
63
98
 
64
- // Unmount React root
65
- if (this.root) {
66
- try {
67
- this.root.unmount();
68
- } catch (error) {
69
- console.error("❌ MathRichInput: Error unmounting React root:", error);
70
- } finally {
71
- this.root = null;
99
+ // Unmount React root
100
+ if (this.root) {
101
+ try {
102
+ this.root.unmount();
103
+ } catch (error) {
104
+ console.error("❌ MathRichInput: Error unmounting React root:", error);
105
+ } finally {
106
+ this.root = null;
107
+ }
72
108
  }
73
- }
109
+ // Long enough for a framework to finish a move it makes in more than one step — a reorder can
110
+ // remove the row and put it back in separate turns, and a teardown landing between the two rebuilds
111
+ // the field for nothing. A genuine removal simply tears down a fraction of a second later.
112
+ }, 60);
74
113
  }
75
114
 
76
115
  static get observedAttributes() {