Skip to content

Commit 2f433ed

Browse files
SVG text instead of div
1 parent 202e9a1 commit 2f433ed

File tree

3 files changed

+48
-44
lines changed

3 files changed

+48
-44
lines changed

client/src/components/node.rs

Lines changed: 36 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use crate::data::{NodeProperty, Store};
1+
use crate::data::node::measure_line_height;
2+
use crate::data::{NodeProperty, Store, FONT_SIZE, TEXT_PADDING};
23
use dioxus::prelude::*;
34
use std::rc::Rc;
45
use uuid::Uuid;
@@ -11,7 +12,6 @@ pub fn DraggedNode(id: Uuid, coords: (f32, f32)) -> Element {
1112
g { transform: format!("translate({}, {})", coords.0, coords.1),
1213
r#use {
1314
href: format!("#{id}"),
14-
style: "transform: scale(0.5); opacity: 0.3;",
1515
}
1616
}
1717
}
@@ -61,6 +61,22 @@ fn RawRootNode(width: f32, height: f32, color: String) -> Element {
6161
}
6262
}
6363

64+
#[component]
65+
fn NodeLabel(label: String) -> Element {
66+
rsx! {
67+
for (index, line) in label.lines().enumerate() {
68+
text {
69+
y: index as f32 * measure_line_height() + TEXT_PADDING,
70+
x: TEXT_PADDING,
71+
text_anchor: "start",
72+
dominant_baseline: "text-before-edge",
73+
font_size: FONT_SIZE,
74+
"{line}"
75+
}
76+
}
77+
}
78+
}
79+
6480
#[component]
6581
fn RawNode(node: crate::data::RenderedNode) -> Element {
6682
let width = node.width();
@@ -184,14 +200,13 @@ pub fn Node(id: Uuid, store: Store) -> Element {
184200
}
185201
}
186202

187-
foreignObject {
188-
x: format!("{}", -width / 2.0),
189-
y: format!("{}", -height / 2.0),
190-
width: format!("{}", width),
191-
height: format!("{}", height),
192-
if *store.pane.editing.read() == Some(node.id) {
203+
if *store.pane.editing.read() == Some(node.id) {
204+
foreignObject {
205+
x: format!("{}", -width / 2.0),
206+
y: format!("{}", -height / 2.0),
207+
width: format!("{}", width),
208+
height: format!("{}", height),
193209
textarea {
194-
style: if store.pane.dragging_node.read().is_some() { "pointer-events: none;" } else { "" },
195210
key: "{id}-textarea",
196211
onmounted: move |element| input_element.set(Some(element.data())),
197212
value: "{node.text}",
@@ -202,51 +217,32 @@ pub fn Node(id: Uuid, store: Store) -> Element {
202217
tabindex: -1,
203218
style: "
204219
user-select: none;
205-
margin: 0px 10px;
206-
padding: 9px 0px;
220+
margin: 0px;
221+
padding: {TEXT_PADDING}px {TEXT_PADDING}px;
207222
font-family: inherit;
208-
width: 100%;
209-
height: 100%;
223+
width: {width}px;
224+
height: {height}px;
210225
outline:none;
211226
background: transparent;
212227
border: none;
213228
resize:none;
214229
overflow:hidden;
215230
font-size: {font_size}px;
216231
display: block;
217-
line-height: 1.2",
232+
line-height: {measure_line_height() + 0.3}px",
218233
oninput: move |evt| {
219234
store.graph.update_node(id, NodeProperty::Text(evt.value().clone()));
220235
},
221236
}
222-
} else {
223-
div {
224-
style: "
225-
vertical-align: top;
226-
line-height: 1.2;
227-
padding-left: 2px;
228-
display: flex;
229-
justify-content: center;
230-
align-items: center;
231-
width: 100%;
232-
height: 100%;
233-
overflow: hidden;
234-
white-space: pre-wrap;
235-
word-wrap: break-word;
236-
text-align: center;
237-
font-size: {font_size}px;
238-
background: transparent;
239-
color: black;
240-
pointer-events: none;
241-
-webkit-user-select: none;
242-
-moz-user-select: none;
243-
-ms-user-select: none;
244-
user-select: none;
245-
font-family: inherit;",
246-
{node.text}
247-
}
248237
}
238+
} else {
239+
g {
240+
transform: format!("translate({},{})", -width / 2.0, -height / 2.0),
241+
242+
NodeLabel { label: node.text }
243+
}
249244
}
245+
250246
}
251247
}
252248
}

client/src/data/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,5 @@ pub mod connection;
2121
pub use connection::Connection;
2222

2323
pub const DEFAULT_COLOR: &str = "#bdb2ff";
24+
pub const FONT_SIZE: f32 = 14.0;
25+
pub const TEXT_PADDING: f32 = 10.0;

client/src/data/node.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::DEFAULT_COLOR;
1+
use super::{DEFAULT_COLOR, FONT_SIZE, TEXT_PADDING};
22
use std::sync::OnceLock;
33
use uuid::Uuid;
44

@@ -10,7 +10,6 @@ fn get_font() -> &'static Font {
1010
Font::from_bytes(FONT_BYTES, fontdue::FontSettings::default()).expect("Failed to load font")
1111
})
1212
}
13-
const FONT_SIZE: f32 = 14.0;
1413

1514
pub fn measure_text_width(text: &str) -> f32 {
1615
text.chars()
@@ -21,6 +20,14 @@ pub fn measure_text_width(text: &str) -> f32 {
2120
.sum()
2221
}
2322

23+
pub fn measure_line_height() -> f32 {
24+
get_font()
25+
.horizontal_line_metrics(FONT_SIZE)
26+
.unwrap()
27+
.new_line_size
28+
.ceil()
29+
}
30+
2431
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
2532
pub enum RelativeLocation {
2633
Top,
@@ -44,7 +51,6 @@ pub struct RenderedNode {
4451
pub progress: i64,
4552
}
4653

47-
const TEXT_PADDING: f32 = 10.0;
4854
impl RenderedNode {
4955
pub fn new(
5056
id: Uuid,
@@ -85,7 +91,7 @@ impl RenderedNode {
8591
self.text.lines().count()
8692
}
8793
.max(1);
88-
FONT_SIZE * lines as f32 + FONT_SIZE * 0.2 * (lines as f32 - 1.0) + TEXT_PADDING * 2.0
94+
lines as f32 * measure_line_height() + TEXT_PADDING * 2.0
8995
}
9096

9197
pub fn font_size(&self) -> f32 {

0 commit comments

Comments
 (0)