diff --git a/client/app.tsx b/client/app.tsx index 4125e441..be54e853 100644 --- a/client/app.tsx +++ b/client/app.tsx @@ -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); @@ -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(); +}); diff --git a/client/controller.ts b/client/controller.ts index 2f715879..9d039bf0 100644 --- a/client/controller.ts +++ b/client/controller.ts @@ -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), diff --git a/client/draw/select.ts b/client/draw/select.ts index b54040e6..05dbfaaa 100644 --- a/client/draw/select.ts +++ b/client/draw/select.ts @@ -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) { diff --git a/client/draw/text.ts b/client/draw/text.ts index bdb351e1..6db35ae3 100644 --- a/client/draw/text.ts +++ b/client/draw/text.ts @@ -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;