Skip to content

Commit

Permalink
Add initial <textarea> support
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoburns committed Dec 27, 2024
1 parent 6d3733d commit 1d78c3f
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 7 deletions.
2 changes: 1 addition & 1 deletion packages/blitz-dom/src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ impl DocumentLike for Document {
if let Some(input_data) = text_input_data {
println!("Sent text event to {}", node_id);
apply_keypress_event(
&mut input_data.editor,
input_data,
&mut self.font_ctx,
&mut self.layout_ctx,
event,
Expand Down
13 changes: 8 additions & 5 deletions packages/blitz-dom/src/events/keyboard.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use crate::node::TextBrush;
use parley::{FontContext, LayoutContext, PlainEditor};
use crate::node::{TextBrush, TextInputData};
use parley::{FontContext, LayoutContext};
use winit::{
event::{KeyEvent, Modifiers},
keyboard::{Key, NamedKey},
};

pub(crate) fn apply_keypress_event(
editor: &mut PlainEditor<TextBrush>,
input_data: &mut TextInputData,
font_ctx: &mut FontContext,
layout_ctx: &mut LayoutContext<TextBrush>,
event: KeyEvent,
Expand All @@ -26,6 +26,8 @@ pub(crate) fn apply_keypress_event(
}
};

let is_multiline = input_data.is_multiline;
let editor = &mut input_data.editor;
let mut driver = editor.driver(font_ctx, layout_ctx);
match event.logical_key {
#[cfg(all(feature = "clipboard", not(target_os = "android")))]
Expand Down Expand Up @@ -142,8 +144,9 @@ pub(crate) fn apply_keypress_event(
}
}
Key::Named(NamedKey::Enter) => {
// TODO: support multi-line text inputs
// driver.insert_or_replace_selection("\n")
if is_multiline {
driver.insert_or_replace_selection("\n");
}
}
Key::Named(NamedKey::Space) => driver.insert_or_replace_selection(" "),
Key::Character(s) => driver.insert_or_replace_selection(&s),
Expand Down
1 change: 1 addition & 0 deletions packages/blitz-dom/src/layout/construct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -875,6 +875,7 @@ pub(crate) fn build_inline_layout(
if *tag_name == local_name!("img")
|| *tag_name == local_name!("svg")
|| *tag_name == local_name!("input")
|| *tag_name == local_name!("textarea")
{
builder.push_inline_box(InlineBox {
id: node_id as u64,
Expand Down
13 changes: 12 additions & 1 deletion packages/blitz-dom/src/layout/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,18 @@ impl LayoutPartialTree for Document {
return taffy::LayoutOutput::HIDDEN;
}

// todo: need to handle shadow roots by actually descending into them
// TODO: deduplicate with single-line text input
if *element_data.name.local == *"textarea" {
return compute_leaf_layout(
inputs,
&node.style,
|_known_size, _available_space| taffy::Size {
width: 300.0,
height: resolved_line_height.unwrap_or(16.0) * 4.0,
},
);
}

if *element_data.name.local == *"input" {
match element_data.attr(local_name!("type")) {
// if the input type is hidden, hide it
Expand Down

0 comments on commit 1d78c3f

Please sign in to comment.