Skip to content

Commit

Permalink
feat: Clipboard integration in use_editable hook
Browse files Browse the repository at this point in the history
  • Loading branch information
marc2332 committed Nov 19, 2023
1 parent 8220b9a commit 3966601
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 4 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ dioxus-hooks = { version = "0.4" }
dioxus-core = { version = "0.4" }
dioxus-hot-reload = { version = "0.4", features = ["file_watcher"] }
dioxus-router = { version = "0.4", default-features = false }
dioxus-std = { version = "0.4", features = ["clipboard"] }

skia-safe = { version = "0.67.0", features = ["gl", "textlayout", "svg"] }

Expand Down Expand Up @@ -66,7 +67,7 @@ freya-node-state = { workspace = true }
reqwest = { version = "0.11.22", features = ["json"] }
serde = "1.0.189"
tracing-subscriber = "0.3.17"
dioxus-std = { version = "0.4", features = ["i18n"] }
dioxus-std = { version = "0.4", features = ["i18n", "clipboard"] }
rand = "0.8.5"
dioxus-router = { workspace = true }
itertools = "0.11.0"
Expand Down
1 change: 1 addition & 0 deletions crates/hooks/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ dioxus-native-core = { workspace = true }
dioxus-core-macro = { workspace = true }
dioxus-hooks = { workspace = true }
dioxus-core = { workspace = true }
dioxus-std = { workspace = true }

tokio = { workspace = true }
winit = { workspace = true }
Expand Down
16 changes: 15 additions & 1 deletion crates/hooks/src/rope_editor.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{cmp::Ordering, fmt::Display, ops::Range};

use dioxus_std::clipboard::UseClipboard;
use ropey::iter::Lines;
pub use ropey::Rope;

Expand All @@ -14,6 +15,8 @@ pub struct RopeEditor {

/// Selected text range
selected: Option<(usize, usize)>,

clipboard: UseClipboard
}

impl Display for RopeEditor {
Expand All @@ -24,12 +27,13 @@ impl Display for RopeEditor {

impl RopeEditor {
// Create a new [`RopeEditor`]
pub fn new(text: String, cursor: TextCursor, mode: EditableMode) -> Self {
pub fn new(text: String, cursor: TextCursor, mode: EditableMode, clipboard: UseClipboard) -> Self {
Self {
rope: Rope::from_str(&text),
cursor,
selected: None,
mode,
clipboard
}
}

Expand Down Expand Up @@ -93,6 +97,10 @@ impl TextEditor for RopeEditor {
}
}

fn get_clipboard(&self) -> &UseClipboard {
&self.clipboard
}

fn highlights(&self, editor_id: usize) -> Option<(usize, usize)> {
let (selected_from, selected_to) = self.selected?;

Expand Down Expand Up @@ -190,6 +198,12 @@ impl TextEditor for RopeEditor {
self.set_cursor_pos(to);
}
}

fn get_selected_text(&self) -> Option<String> {
let selected = self.selected?;

Some(self.rope().get_slice(selected.0..selected.1)?.to_string())
}
}

/// Iterator over text lines.
Expand Down
11 changes: 10 additions & 1 deletion crates/hooks/src/text_editor.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{borrow::Cow, fmt::Display, ops::Range};

use dioxus_std::clipboard::UseClipboard;
use freya_elements::events::keyboard::{Code, Key, Modifiers};
pub use ropey::Rope;

Expand Down Expand Up @@ -179,6 +180,8 @@ pub trait TextEditor: Sized + Clone + Display {

fn move_highlight_to_cursor(&mut self);

fn get_clipboard(&self) -> &UseClipboard;

// Process a Keyboard event
fn process_key(&mut self, key: &Key, code: &Code, modifiers: &Modifiers) -> TextEvent {
let mut event = TextEvent::None;
Expand Down Expand Up @@ -358,12 +361,16 @@ pub trait TextEditor: Sized + Clone + Display {

event = TextEvent::TextChanged
}
Code::KeyC if modifiers.contains(Modifiers::CONTROL) => {
let selected = self.get_selected_text();
println!("copying: >{selected:?}<");
}
_ => {
if let Ok(ch) = character.parse::<char>() {
if !ch.is_ascii_control() {
// Adds a new character
let char_idx =
self.line_to_char(self.cursor_row()) + self.cursor_col();
self.line_to_char(self.cursor_row()) + self.cursor_col();
self.insert(character, char_idx);
self.cursor_right();

Expand All @@ -382,4 +389,6 @@ pub trait TextEditor: Sized + Clone + Display {

event
}

fn get_selected_text(&self) -> Option<String>;
}
4 changes: 3 additions & 1 deletion crates/hooks/src/use_editable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::{

use dioxus_core::{AttributeValue, Scope, ScopeState};
use dioxus_hooks::{use_effect, use_ref, use_state, UseRef, UseState};
use dioxus_std::clipboard::use_clipboard;
use freya_common::{CursorLayoutResponse, EventMessage};
use freya_elements::events::{KeyboardData, MouseData};
use freya_node_state::{CursorReference, CustomAttributeValues};
Expand Down Expand Up @@ -157,11 +158,12 @@ pub fn use_editable(
) -> UseEditable {
let id = cx.use_hook(Uuid::new_v4);
let platform = use_platform(cx);
let clipboard = use_clipboard(cx);

// Hold the text editor
let text_editor = use_state(cx, || {
let config = initializer();
RopeEditor::new(config.content, config.cursor, mode)
RopeEditor::new(config.content, config.cursor, mode, clipboard)
});

let cursor_channels = cx.use_hook(|| {
Expand Down

0 comments on commit 3966601

Please sign in to comment.