Skip to content

Commit aedcfd6

Browse files
authored
fix: Use a readonly textarea for non-editable comments. (#8632)
* fix: Use a readonly textarea for non-editable comments. * chore: Run formatter. * chore: remove old function definition
1 parent d053008 commit aedcfd6

3 files changed

Lines changed: 32 additions & 25 deletions

File tree

core/bubbles/textinput_bubble.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ export class TextInputBubble extends Bubble {
6363
20 + Bubble.DOUBLE_BORDER,
6464
);
6565

66+
private editable = true;
67+
6668
/**
6769
* @param workspace The workspace this bubble belongs to.
6870
* @param anchor The anchor location of the thing this bubble is attached to.
@@ -96,6 +98,21 @@ export class TextInputBubble extends Bubble {
9698
this.onTextChange();
9799
}
98100

101+
/** Sets whether or not the text in the bubble is editable. */
102+
setEditable(editable: boolean) {
103+
this.editable = editable;
104+
if (this.editable) {
105+
this.textArea.removeAttribute('readonly');
106+
} else {
107+
this.textArea.setAttribute('readonly', '');
108+
}
109+
}
110+
111+
/** Returns whether or not the text in the bubble is editable. */
112+
isEditable(): boolean {
113+
return this.editable;
114+
}
115+
99116
/** Adds a change listener to be notified when this bubble's text changes. */
100117
addTextChangeListener(listener: () => void) {
101118
this.textChangeListeners.push(listener);

core/icons/comment_icon.ts

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
import type {Block} from '../block.js';
1010
import type {BlockSvg} from '../block_svg.js';
11-
import {TextBubble} from '../bubbles/text_bubble.js';
1211
import {TextInputBubble} from '../bubbles/textinput_bubble.js';
1312
import {EventType} from '../events/type.js';
1413
import * as eventUtils from '../events/utils.js';
@@ -47,12 +46,9 @@ export class CommentIcon extends Icon implements IHasBubble, ISerializable {
4746
*/
4847
static readonly WEIGHT = 3;
4948

50-
/** The bubble used to show editable text to the user. */
49+
/** The bubble used to show comment text to the user. */
5150
private textInputBubble: TextInputBubble | null = null;
5251

53-
/** The bubble used to show non-editable text to the user. */
54-
private textBubble: TextBubble | null = null;
55-
5652
/** The text of this comment. */
5753
private text = '';
5854

@@ -118,7 +114,6 @@ export class CommentIcon extends Icon implements IHasBubble, ISerializable {
118114
override dispose() {
119115
super.dispose();
120116
this.textInputBubble?.dispose();
121-
this.textBubble?.dispose();
122117
}
123118

124119
override getWeight(): number {
@@ -133,7 +128,6 @@ export class CommentIcon extends Icon implements IHasBubble, ISerializable {
133128
super.applyColour();
134129
const colour = (this.sourceBlock as BlockSvg).style.colourPrimary;
135130
this.textInputBubble?.setColour(colour);
136-
this.textBubble?.setColour(colour);
137131
}
138132

139133
/**
@@ -153,7 +147,6 @@ export class CommentIcon extends Icon implements IHasBubble, ISerializable {
153147
super.onLocationChange(blockOrigin);
154148
const anchorLocation = this.getAnchorLocation();
155149
this.textInputBubble?.setAnchorLocation(anchorLocation);
156-
this.textBubble?.setAnchorLocation(anchorLocation);
157150
}
158151

159152
/** Sets the text of this comment. Updates any bubbles if they are visible. */
@@ -170,7 +163,6 @@ export class CommentIcon extends Icon implements IHasBubble, ISerializable {
170163
);
171164
this.text = text;
172165
this.textInputBubble?.setText(this.text);
173-
this.textBubble?.setText(this.text);
174166
}
175167

176168
/** Returns the text of this comment. */
@@ -302,33 +294,31 @@ export class CommentIcon extends Icon implements IHasBubble, ISerializable {
302294
* to update the state of this icon in response to changes in the bubble.
303295
*/
304296
private showEditableBubble() {
305-
this.textInputBubble = new TextInputBubble(
306-
this.sourceBlock.workspace as WorkspaceSvg,
307-
this.getAnchorLocation(),
308-
this.getBubbleOwnerRect(),
309-
);
310-
this.textInputBubble.setText(this.getText());
311-
this.textInputBubble.setSize(this.bubbleSize, true);
312-
this.textInputBubble.addTextChangeListener(() => this.onTextChange());
313-
this.textInputBubble.addSizeChangeListener(() => this.onSizeChange());
297+
this.createBubble();
298+
this.textInputBubble?.addTextChangeListener(() => this.onTextChange());
299+
this.textInputBubble?.addSizeChangeListener(() => this.onSizeChange());
314300
}
315301

316302
/** Shows the non editable text bubble for this comment. */
317303
private showNonEditableBubble() {
318-
this.textBubble = new TextBubble(
319-
this.getText(),
304+
this.createBubble();
305+
this.textInputBubble?.setEditable(false);
306+
}
307+
308+
protected createBubble() {
309+
this.textInputBubble = new TextInputBubble(
320310
this.sourceBlock.workspace as WorkspaceSvg,
321311
this.getAnchorLocation(),
322312
this.getBubbleOwnerRect(),
323313
);
314+
this.textInputBubble.setText(this.getText());
315+
this.textInputBubble.setSize(this.bubbleSize, true);
324316
}
325317

326318
/** Hides any open bubbles owned by this comment. */
327319
private hideBubble() {
328320
this.textInputBubble?.dispose();
329321
this.textInputBubble = null;
330-
this.textBubble?.dispose();
331-
this.textBubble = null;
332322
}
333323

334324
/**

tests/mocha/comment_test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ suite('Comments', function () {
4141
});
4242

4343
function assertEditable(comment) {
44-
assert.isNotOk(comment.textBubble);
4544
assert.isOk(comment.textInputBubble);
45+
assert.isTrue(comment.textInputBubble.isEditable());
4646
}
4747
function assertNotEditable(comment) {
48-
assert.isNotOk(comment.textInputBubble);
49-
assert.isOk(comment.textBubble);
48+
assert.isOk(comment.textInputBubble);
49+
assert.isFalse(comment.textInputBubble.isEditable());
5050
}
5151
test('Editable', async function () {
5252
await this.comment.setBubbleVisible(true);

0 commit comments

Comments
 (0)