Code Monkey home page Code Monkey logo

textr's Introduction

Text Selection Highlighter

I went down a rabbit hole after watching a ThePrimeagen video wherein it was suggested that devs should try and build their own text editor from scratch. It sounded like a juicy little challenge and this is my effort. It will be a WIP for a bit. I'll change this README when I'm (kinda) done.

NOTE: Like many things apparently simple...this ain't!

Features

  • Calculate the position of the mouse cursor relative to the text container
  • Update the current selection as the mouse moves
  • Finalize the selection when the mouse button is released
  • Highlight the selected text with a specified color

Upcoming Features

  • Adding undo/redo functionality...(command pattern?)
  • Rich text editing capabilities
  • Code syntax highlighting
  • Word wrap
  • ???

Usage

Before using the text editor in your project, you need to compile the TypeScript files to JavaScript. Run the following command in your project directory:

tsc editor.ts

This will generate an editor.js file that you should include in your HTML.

Include the JavaScript file in your project and call the functions as needed. The main functions are:

  • calculatePosition(x: number, y: number): number - Calculates the position of the mouse cursor relative to the text container.
  • updateSelection(event: MouseEvent): void - Updates the current selection as the mouse moves.
  • endSelection(event: MouseEvent): void - Finalizes the selection when the mouse button is released.
  • highlightSelection(startPos: number, endPos: number): void - Highlights the selected text with a specified color.

Example

const editor = document.getElementById("editor") as HTMLDivElement;
editor.addEventListener("mousedown", startSelection);
editor.addEventListener("mousemove", updateSelection);
editor.addEventListener("mouseup", endSelection);

textr's People

Contributors

sethderrick avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.