Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support pasting in all tools #280

Merged
merged 12 commits into from
Feb 23, 2024
27 changes: 24 additions & 3 deletions client/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ import styles from "#asciiflow/client/app.module.css";
import {
Controller,
DesktopController,
TouchController
TouchController,
} from "#asciiflow/client/controller";
import { Drawer } from "#asciiflow/client/drawer";
import { DrawingId, store } from "#asciiflow/client/store";
import { View } from "#asciiflow/client/view";
import { DrawingId, store, ToolMode } from "#asciiflow/client/store";
import { screenToCell, View } from "#asciiflow/client/view";
import { useObserver } from "mobx-react";
import { HashRouter, Route, useParams } from "react-router-dom";
import * as ReactDOM from "react-dom";
import { Vector } from "#asciiflow/client/vector";
import { textToLayer } from "#asciiflow/client/text_utils";

const controller = new Controller();
const touchController = new TouchController(controller);
Expand Down Expand Up @@ -73,3 +75,22 @@ render().catch((e) => console.log(e));
window.addEventListener("keypress", (e) => controller.handleKeyPress(e));
window.addEventListener("keydown", (e) => controller.handleKeyDown(e));
window.addEventListener("keyup", (e) => controller.handleKeyUp(e));

window.document.addEventListener("paste", (e) => {
e.preventDefault();
// Text tool manages pasting it's own way.
const clipboardText = e.clipboardData.getData("text");
// Default to the center of the screen.
var position = screenToCell(new Vector(window.innerWidth / 2, window.innerHeight / 2));
// Use the select tool position if set.
if (store.selectTool.selectBox) {
position = store.selectTool.selectBox.topLeft();
}
if (store.toolMode === ToolMode.TEXT && store.textTool.currentPosition) {
position = store.textTool.currentPosition;
}
const pastedLayer = textToLayer(clipboardText, position);
store.currentTool.cleanup();
store.currentCanvas.setScratchLayer(pastedLayer);
store.currentCanvas.commitScratch();
});
2 changes: 0 additions & 2 deletions client/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ export class Controller {
}

handleKeyPress(event: KeyboardEvent) {
console.log(event.keyCode);

if (!event.ctrlKey && !event.metaKey && event.keyCode !== 13) {
store.currentTool.handleKey(
String.fromCharCode(event.keyCode),
Expand Down
13 changes: 1 addition & 12 deletions client/draw/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,13 @@ import { Vector } from "#asciiflow/client/vector";
export class DrawSelect extends AbstractDrawFunction {
private moveTool: DrawMove;

private selectBox: Box;
public selectBox: Box;

private dragStart: Vector;
private dragEnd: Vector;

constructor() {
super();
window.document.addEventListener("paste", (e) => {
const clipboardText = e.clipboardData.getData("text");
if (this.selectBox) {
const pastedLayer = textToLayer(
clipboardText,
this.selectBox.topLeft()
);
store.currentCanvas.setScratchLayer(pastedLayer);
store.currentCanvas.commitScratch();
}
});
}

start(position: Vector, modifierKeys: IModifierKeys) {
Expand Down
2 changes: 1 addition & 1 deletion client/draw/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { store, IModifierKeys } from "#asciiflow/client/store";
import { Vector } from "#asciiflow/client/vector";

export class DrawText extends AbstractDrawFunction {
private currentPosition: Vector;
public currentPosition: Vector;
private textLayer: Layer;
private newLineAlignment: Vector;

Expand Down
Loading