Skip to content

Commit b1fdce6

Browse files
authored
Merge pull request #6 from ETH-PEACH-Lab/feat/add-set-text-and-text-dot-accessing
feat: add setText method and examples for text positioning in data st…
2 parents 01275fb + 32e5f10 commit b1fdce6

8 files changed

Lines changed: 190 additions & 10 deletions

File tree

docs/data-structures/array.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ Arrays support the following properties:
4343

4444
Arrays support these methods for manipulation:
4545

46+
### Text Positioning Methods
47+
- `setText(text, position)` - Set or remove text at specific positions (`"above"`, `"below"`, `"left"`, `"right"`)
48+
4649
### Single Element Methods
4750
- `setValue(index, value)` - Set value at specific index
4851
- `setColor(index, color)` - Set color at specific index
@@ -147,6 +150,38 @@ show scores
147150
`}
148151
</SideBySide>
149152

153+
### Dynamic Text with setText
154+
155+
<SideBySide
156+
language="merlin"
157+
bordered={true}
158+
diagramWidth={400}
159+
diagramHeight={300}
160+
overrideSize={true}
161+
>
162+
{`
163+
array arr = {
164+
value: [1, 2, 3, 4]
165+
}
166+
167+
page
168+
show arr
169+
170+
page
171+
arr.setText("Array Elements", "above")
172+
arr.setText("Index: 0, 1, 2, 3", "below")
173+
174+
page
175+
arr.setText("Modified Array", "above")
176+
arr.above.setFontWeight("bold")
177+
arr.below.setColor("gray")
178+
179+
page
180+
arr.setText(null, "below") // remove bottom text
181+
arr.setText("Size: 4", "right")
182+
`}
183+
</SideBySide>
184+
150185
## Use Cases
151186

152187
Arrays are ideal for:

docs/data-structures/graph.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ Graphs support the following properties:
4848

4949
Graphs support these methods for manipulation:
5050

51+
### Text Positioning Methods
52+
- `setText(text, position)` - Set or remove text at specific positions (`"above"`, `"below"`, `"left"`, `"right"`)
53+
5154
### Node Methods
5255
- `addNode(name, value?)` - Add new node with optional value
5356
- `removeNode(name)` - Remove node and its edges

docs/data-structures/linkedlist.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ LinkedLists support the following properties:
4545

4646
LinkedLists support these methods for manipulation:
4747

48+
### Text Positioning Methods
49+
- `setText(text, position)` - Set or remove text at specific positions (`"above"`, `"below"`, `"left"`, `"right"`)
50+
4851
### Node Methods
4952
- `addNode(name, value?)` - Add new node with optional value
5053
- `insertNode(index | id, name, value?)` - Insert node at specific position or after node ID with optional value

docs/data-structures/matrix.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ Matrices support the following properties:
4343

4444
Matrices support these methods for manipulation:
4545

46+
### Text Positioning Methods
47+
- `setText(text, position)` - Set or remove text at specific positions (`"above"`, `"below"`, `"left"`, `"right"`)
48+
4649
### Single Element Methods
4750
- `setValue(row, col, value)` - Set value at specific position
4851
- `setColor(row, col, color)` - Set color at specific position
@@ -171,6 +174,38 @@ show myMatrix
171174
`}
172175
</SideBySide>
173176

177+
### Dynamic Text with setText
178+
179+
<SideBySide
180+
language="merlin"
181+
bordered={true}
182+
diagramWidth={400}
183+
diagramHeight={350}
184+
overrideSize={true}
185+
>
186+
{`
187+
matrix data = {
188+
value: [[1, 2], [3, 4]]
189+
}
190+
191+
page
192+
show data
193+
194+
page
195+
data.setText("Matrix Title", "above")
196+
data.setText("Row Labels", "left")
197+
198+
page
199+
data.setText("Updated Title", "above")
200+
data.above.setFontSize(18)
201+
data.above.setColor("blue")
202+
203+
page
204+
data.setText(null, "left") // remove left text
205+
data.setText("Column Info", "below")
206+
`}
207+
</SideBySide>
208+
174209
## Use Cases
175210

176211
Matrices are ideal for:

docs/data-structures/stack.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ Stacks support the following properties:
4343

4444
Stacks support these methods for manipulation:
4545

46+
### Text Positioning Methods
47+
- `setText(text, position)` - Set or remove text at specific positions (`"above"`, `"below"`, `"left"`, `"right"`)
48+
4649
### Single Element Methods
4750
- `setValue(index, value)` - Set value at specific position
4851
- `setColor(index, color)` - Set color at specific position

docs/data-structures/text.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,13 @@ Text elements support the following properties:
6060

6161
Text elements support these methods for manipulation:
6262

63+
:::info
64+
Text objects do not support the `setText()` method since they are text themselves. Use `setValue()`, `setFontSize()`, and other text-specific methods instead.
65+
:::
66+
6367
### Single Property Methods
68+
- `setValue(value)` - Set text content for all lines
69+
- `setValue(line, value)` - Set text content for specific line
6470
- `setFontSize(value)` - Set font size for all text
6571
- `setFontSize(line, size)` - Set font size for specific line
6672
- `setColor(line, color)` - Set color for specific line
@@ -75,6 +81,7 @@ Text elements support these methods for manipulation:
7581
- `setHeight(height)` - Set text box height
7682

7783
### Multiple Element Methods
84+
- `setValues([...])` - Set text content for multiple lines
7885
- `setFontSizes([...])` - Set font sizes for multiple lines
7986
- `setColors([...])` - Set colors for multiple lines
8087
- `setFontWeights([...])` - Set font weights for multiple lines
@@ -152,6 +159,35 @@ status.setFontWeight("bold")
152159
`}
153160
</SideBySide>
154161

162+
### Multiple Lines with setValues
163+
164+
<SideBySide
165+
language="merlin"
166+
bordered={true}
167+
diagramWidth={400}
168+
diagramHeight={300}
169+
overrideSize={true}
170+
>
171+
{`
172+
text progress = {
173+
value: ["Step 1: Initialize", "Step 2: Process", "Step 3: Complete"]
174+
fontSize: 14
175+
color: ["gray", "gray", "gray"]
176+
}
177+
178+
page
179+
show progress
180+
181+
page
182+
progress.setValues(["Step 1: Initialize ✓", "Step 2: Process...", "Step 3: Complete"])
183+
progress.setColors(["green", "orange", "gray"])
184+
185+
page
186+
progress.setValues(["Step 1: Initialize ✓", "Step 2: Process ✓", "Step 3: Complete ✓"])
187+
progress.setColors(["green", "green", "green"])
188+
`}
189+
</SideBySide>
190+
155191
### Text as Data Labels
156192

157193
<SideBySide

docs/data-structures/tree.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ Trees support the following properties:
4646

4747
Trees support these methods for manipulation:
4848

49+
### Text Positioning Methods
50+
- `setText(text, position)` - Set or remove text at specific positions (`"above"`, `"below"`, `"left"`, `"right"`)
51+
4952
### Node Methods
5053
- `addNode(name, value?)` - Add new node with optional value
5154
- `removeNode(name)` - Remove node and its subtree

docs/methods.md

Lines changed: 72 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,30 @@ matrix.setValue(1, 2, "newValue")
8888

8989
---
9090

91+
### `setValue(value)`
92+
Sets the text content for all lines.
93+
94+
**Syntax:**
95+
```merlin
96+
text.setValue("newText")
97+
```
98+
99+
**Supported by:** Text
100+
101+
---
102+
103+
### `setValue(line, value)`
104+
Sets the text content for a specific line.
105+
106+
**Syntax:**
107+
```merlin
108+
text.setValue(0, "newText") // first line
109+
```
110+
111+
**Supported by:** Text
112+
113+
---
114+
91115
### `setColor(index, color)`
92116
Sets the color at a specific index.
93117

@@ -192,7 +216,7 @@ obj.setValues([1, 2, 3])
192216
obj.setValues([_, 2, _]) // only change middle value
193217
```
194218

195-
**Supported by:** Array, Stack, LinkedList, Graph, Tree
219+
**Supported by:** Array, Stack, LinkedList, Graph, Tree, Text
196220

197221
---
198222

@@ -271,6 +295,44 @@ graph.setHidden([true, false, true])
271295

272296
---
273297

298+
## Text Positioning Methods
299+
300+
### `setText(text, position)`
301+
Sets or removes text at a specific position around a data structure.
302+
303+
**Syntax:**
304+
```merlin
305+
obj.setText("text", "above")
306+
obj.setText("text", "below")
307+
obj.setText("text", "left")
308+
obj.setText("text", "right")
309+
obj.setText(null, "above") // remove text
310+
```
311+
312+
**Parameters:**
313+
- `text`: String value to set, or `null` to remove text
314+
- `position`: String literal indicating placement - `"above"`, `"below"`, `"left"`, or `"right"`
315+
316+
**Behavior:**
317+
- Creates a new text component if none exists at the specified position
318+
- Updates existing text component if one exists at the position
319+
- Removes text component when `text` is `null`
320+
- Automatically handles text object lifecycle (creation/deletion)
321+
322+
**Chained Method Access:**
323+
Access methods of linked text objects through position chaining:
324+
```merlin
325+
// Access text object methods through the main object
326+
obj.above.setFontSize(16)
327+
obj.below.setColor("#ff0000")
328+
obj.left.setValue("New Text")
329+
obj.right.setFontWeight("bold")
330+
```
331+
332+
**Supported by:** Array, Matrix, Graph, Tree, Stack, LinkedList (all non-text structures)
333+
334+
---
335+
274336
## Add/Insert Methods
275337

276338
### `addValue(value)`
@@ -737,12 +799,12 @@ text.setAligns(["left", "center", "right"])
737799

738800
## Method Compatibility
739801

740-
| Data Structure | setValue | setColor | setArrow | add/insert | remove | Special Methods |
741-
|---------------|----------|----------|----------|------------|--------|----------------|
742-
| Array | ✓ (index) | ✓ (index) | ✓ (index) ||| - |
743-
| Matrix | ✓ (2D) | ✓ (2D) | ✓ (2D) | - | - | addRow, addColumn, addBorder |
744-
| Graph | ✓ (index/id) | ✓ (index/id) | ✓ (index/id) ||| setHidden (index/id), addEdge, setEdges |
745-
| Tree | ✓ (index/id) | ✓ (index/id) | ✓ (index/id) ||| addChild, setChild, removeSubtree |
746-
| Stack | ✓ (index) | ✓ (index) | ✓ (index) ||| - |
747-
| LinkedList | ✓ (index/id) | ✓ (index/id) | ✓ (index/id) ||| addNode, insertNode |
748-
| Text ||| - | - | - | Font methods, alignment, sizing |
802+
| Data Structure | setValue | setColor | setArrow | setText | add/insert | remove | Special Methods |
803+
|---------------|----------|----------|----------|---------|------------|--------|----------------|
804+
| Array | ✓ (index) | ✓ (index) | ✓ (index) ||| | - |
805+
| Matrix | ✓ (2D) | ✓ (2D) | ✓ (2D) | | - | - | addRow, addColumn, addBorder |
806+
| Graph | ✓ (index/id) | ✓ (index/id) | ✓ (index/id) ||| | setHidden (index/id), addEdge, setEdges |
807+
| Tree | ✓ (index/id) | ✓ (index/id) | ✓ (index/id) ||| | addChild, setChild, removeSubtree |
808+
| Stack | ✓ (index) | ✓ (index) | ✓ (index) ||| | - |
809+
| LinkedList | ✓ (index/id) | ✓ (index/id) | ✓ (index/id) ||| | addNode, insertNode |
810+
| Text ||| - | - | - | - | Font methods, alignment, sizing |

0 commit comments

Comments
 (0)