Skip to content

Commit 1b49307

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

File tree

3 files changed

+48
-47
lines changed

3 files changed

+48
-47
lines changed

client/src/components/node.rs

Lines changed: 36 additions & 43 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;
@@ -9,10 +10,7 @@ const SELECTED_PADDING: f32 = 5.0;
910
pub fn DraggedNode(id: Uuid, coords: (f32, f32)) -> Element {
1011
rsx! {
1112
g { transform: format!("translate({}, {})", coords.0, coords.1),
12-
r#use {
13-
href: format!("#{id}"),
14-
style: "transform: scale(0.5); opacity: 0.3;",
15-
}
13+
r#use { href: format!("#{id}") }
1614
}
1715
}
1816
}
@@ -61,6 +59,22 @@ fn RawRootNode(width: f32, height: f32, color: String) -> Element {
6159
}
6260
}
6361

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

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) {
201+
if *store.pane.editing.read() == Some(node.id) {
202+
foreignObject {
203+
x: format!("{}", -width / 2.0),
204+
y: format!("{}", -height / 2.0),
205+
width: format!("{}", width),
206+
height: format!("{}", height),
193207
textarea {
194-
style: if store.pane.dragging_node.read().is_some() { "pointer-events: none;" } else { "" },
195208
key: "{id}-textarea",
196209
onmounted: move |element| input_element.set(Some(element.data())),
197210
value: "{node.text}",
@@ -202,51 +215,31 @@ pub fn Node(id: Uuid, store: Store) -> Element {
202215
tabindex: -1,
203216
style: "
204217
user-select: none;
205-
margin: 0px 10px;
206-
padding: 9px 0px;
218+
margin: 0px;
219+
padding: {TEXT_PADDING}px {TEXT_PADDING}px;
207220
font-family: inherit;
208-
width: 100%;
209-
height: 100%;
221+
width: {width}px;
222+
height: {height}px;
210223
outline:none;
211224
background: transparent;
212225
border: none;
213226
resize:none;
214227
overflow:hidden;
215228
font-size: {font_size}px;
216229
display: block;
217-
line-height: 1.2",
230+
line-height: {measure_line_height() + 0.3}px",
218231
oninput: move |evt| {
219232
store.graph.update_node(id, NodeProperty::Text(evt.value().clone()));
220233
},
221234
}
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-
}
235+
}
236+
} else {
237+
g { transform: format!("translate({},{})", -width / 2.0, -height / 2.0),
238+
239+
NodeLabel { label: node.text }
248240
}
249241
}
242+
250243
}
251244
}
252245
}

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)