UPDATE: Implement copy/paste in the UI
This commit is contained in:
committed by
Nicolás Hatcher Andrés
parent
843d8beb02
commit
cd54389e91
@@ -137,6 +137,52 @@ set_area_border_types = r"""
|
||||
setAreaWithBorder(area: Area, border_area: BorderArea): void;
|
||||
"""
|
||||
|
||||
paste_csv_string = r"""
|
||||
/**
|
||||
* @param {any} area
|
||||
* @param {string} csv
|
||||
*/
|
||||
pasteCsvText(area: any, csv: string): void;
|
||||
"""
|
||||
|
||||
paste_csv_string_types = r"""
|
||||
/**
|
||||
* @param {Area} area
|
||||
* @param {string} csv
|
||||
*/
|
||||
pasteCsvText(area: Area, csv: string): void;
|
||||
"""
|
||||
|
||||
clipboard = r"""
|
||||
/**
|
||||
* @returns {any}
|
||||
*/
|
||||
copyToClipboard(): any;
|
||||
"""
|
||||
|
||||
clipboard_types = r"""
|
||||
/**
|
||||
* @returns {Clipboard}
|
||||
*/
|
||||
copyToClipboard(): Clipboard;
|
||||
"""
|
||||
|
||||
paste_from_clipboard = r"""
|
||||
/**
|
||||
* @param {any} source_range
|
||||
* @param {any} clipboard
|
||||
*/
|
||||
pasteFromClipboard(source_range: any, clipboard: any): void;
|
||||
"""
|
||||
|
||||
paste_from_clipboard_types = r"""
|
||||
/**
|
||||
* @param {[number, number, number, number]} source_range
|
||||
* @param {ClipboardData} clipboard
|
||||
*/
|
||||
pasteFromClipboard(source_range: [number, number, number, number], clipboard: ClipboardData): void;
|
||||
"""
|
||||
|
||||
def fix_types(text):
|
||||
text = text.replace(get_tokens_str, get_tokens_str_types)
|
||||
text = text.replace(update_style_str, update_style_str_types)
|
||||
@@ -147,6 +193,9 @@ def fix_types(text):
|
||||
text = text.replace(autofill_columns, autofill_columns_types)
|
||||
text = text.replace(set_cell_style, set_cell_style_types)
|
||||
text = text.replace(set_area_border, set_area_border_types)
|
||||
text = text.replace(paste_csv_string, paste_csv_string_types)
|
||||
text = text.replace(clipboard, clipboard_types)
|
||||
text = text.replace(paste_from_clipboard, paste_from_clipboard_types)
|
||||
with open("types.ts") as f:
|
||||
types_str = f.read()
|
||||
header_types = "{}\n\n{}".format(header, types_str)
|
||||
|
||||
@@ -6,7 +6,7 @@ use wasm_bindgen::{
|
||||
use ironcalc_base::{
|
||||
expressions::{lexer::util::get_tokens as tokenizer, types::Area, utils::number_to_column},
|
||||
types::{CellType, Style},
|
||||
BorderArea, UserModel as BaseModel,
|
||||
BorderArea, ClipboardData, UserModel as BaseModel,
|
||||
};
|
||||
|
||||
fn to_js_error(error: String) -> JsError {
|
||||
@@ -497,4 +497,37 @@ impl Model {
|
||||
pub fn set_name(&mut self, name: &str) {
|
||||
self.model.set_name(name);
|
||||
}
|
||||
|
||||
#[wasm_bindgen(js_name = "copyToClipboard")]
|
||||
pub fn copy_to_clipboard(&self) -> Result<JsValue, JsError> {
|
||||
let data = self
|
||||
.model
|
||||
.copy_to_clipboard()
|
||||
.map_err(|e| to_js_error(e.to_string()));
|
||||
data.map(|x| serde_wasm_bindgen::to_value(&x).unwrap())
|
||||
}
|
||||
|
||||
#[wasm_bindgen(js_name = "pasteFromClipboard")]
|
||||
pub fn paste_from_clipboard(
|
||||
&mut self,
|
||||
source_range: JsValue,
|
||||
clipboard: JsValue,
|
||||
) -> Result<(), JsError> {
|
||||
let source_range: (i32, i32, i32, i32) =
|
||||
serde_wasm_bindgen::from_value(source_range).map_err(|e| to_js_error(e.to_string()))?;
|
||||
let clipboard: ClipboardData =
|
||||
serde_wasm_bindgen::from_value(clipboard).map_err(|e| to_js_error(e.to_string()))?;
|
||||
self.model
|
||||
.paste_from_clipboard(source_range, &clipboard)
|
||||
.map_err(|e| to_js_error(e.to_string()))
|
||||
}
|
||||
|
||||
#[wasm_bindgen(js_name = "pasteCsvText")]
|
||||
pub fn paste_csv_string(&mut self, area: JsValue, csv: &str) -> Result<(), JsError> {
|
||||
let range: Area =
|
||||
serde_wasm_bindgen::from_value(area).map_err(|e| to_js_error(e.to_string()))?;
|
||||
self.model
|
||||
.paste_csv_string(&range, csv)
|
||||
.map_err(|e| to_js_error(e.to_string()))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -205,3 +205,23 @@ export interface SelectedView {
|
||||
top_row: number;
|
||||
left_column: number;
|
||||
}
|
||||
|
||||
// type ClipboardData = {
|
||||
// [row: number]: {
|
||||
// [column: number]: ClipboardCell;
|
||||
// };
|
||||
// };
|
||||
|
||||
// type ClipboardData = Record<string, Record <string, ClipboardCell>>;
|
||||
type ClipboardData = Map<number, Map <number, ClipboardCell>>;
|
||||
|
||||
export interface ClipboardCell {
|
||||
text: string;
|
||||
style: CellStyle;
|
||||
}
|
||||
|
||||
export interface Clipboard {
|
||||
csv: string;
|
||||
data: ClipboardData;
|
||||
range: [number, number, number, number];
|
||||
}
|
||||
Reference in New Issue
Block a user