Coder Social home page Coder Social logo

leaverou / rety Goto Github PK

View Code? Open in Web Editor NEW
391.0 4.0 9.0 92 KB

Record typing on one or more editors and replay it at will, to simulate live coding

Home Page: https://rety.verou.me

License: MIT License

JavaScript 66.99% HTML 17.05% CSS 15.96%
live-coding

rety's Introduction

rety

“Live” coding without the stress

What is this?

Rety is a library that allows you to record the edits you make on one or more pieces of text (usually code) and replay them later to recreate the same typing flow.

This is particularly useful for orchestrating live demos that run without your presence.

It does not come with any particular UI, the UI is up to you. The UI you see in some of the demos in these docs is not part of Rety.

Here’s an example of using it together with the Inspire.js Live demo plugin to do live demos during a talk:

<iframe width="100%" style="aspect-ratio: 560 / 315" src="https://www.youtube.com/embed/ZuZizqDF4q8?start=436" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>

Background & Motivation

I love live coding as a teaching tool, and over the years it has become part of my trademark speaking style.

When combined with some kind of interactive preview, it allows the speaker to demonstrate not only the final state of a coding snippet, but how you get there, and what the intermediate results are. Live coding is to programming what a blackboard is to math or physics.

However, it does create a unique challenge: My live coded slides don't make sense without me. This may be acceptable for a conference talk, which is usually recorded, but not in other contexts, such as teaching a university course, where all instructors need to be able to teach all lectures, and students need to be able to quickly refer to examples shown.

I didn't want to remove live coding from my slides, as I truly believe it is the perfect implementation of the "show, don’t tell" teaching adage, so I thought instead: what if I could record my live coding, and make it replayable? However, doing so manually seemed like cruel and unusual punishment. And thus, rety was born (pronounced like the "rety" in "retype").

After using it extensively for my course at MIT, I started using it during actual conference talks as well, as it was strictly superior to actual live coding: It offered the same progressive development which is the primary benefit of live coding, but none of the fumbling, delays, or mistakes that often come with it. You can watch the first conference talk I did with it, at CSS Day 2022 here (first live demo at 7:15).

Rety is designed to work well with the code editors of Prism Live and CodeFlask but it should work with any <input>, <textarea> or even compatible custom elements.

Basic Usage

To record edits on a textarea (myTextarea):

import Recorder from "https://rety.verou.me/src/recorder.js";

let recorder = new Recorder(myTextarea);
recorder.start();

recorder.addEventListener("actionschange", evt => {
	// recorder.actions has been updated
	// evt.detail contains the new (or in some cases changed) action
});

To replay an array of actions (actionsArray) on a textarea (editor) with default settings:

import Replayer from "https://rety.verou.me/src/replayer.js";

let replayer = new Replayer(editor);
replayer.runAll(actionsArray);

Instead of importing directly from the CDN, you can also use npm:

npm install rety

API

Rety consists of two classes, residing in correspondingly named modules: Recorder and Replayer.

Recorder class

The Recorder class allows you to record actions on one or more <input>, <textarea>, or any recorder-compatible control.

let recorder = new Recorder(source);
recorder.start();

To record actions from a single editor, source can be a reference to that editor. To record actions from multiple editors, pass in an object literal with identifiers as keys and references to the elements as values.

E.g.

let recorder = new Recorder({
	css: document.querySelector("textarea#css"),
	html: document.querySelector("textarea#html")
});

The identifiers can be anything you want, e.g. to record actions in a multi-file editor environment, the ids could be the filenames.

Call recorder.start() to start recording and recorder.pause() to temporarily pause recording.

You will find any recorded actions in recorder.actions.

recorder is a subclass of EventTarget, meaning it emits events. You can listen to the actionschange event to respond to any change in the recorder.actions array.

Most changes to recorder.actions will be new actions being added at the end of the array. However, there are few cases where instead of adding a new action, the previous action is modified instead. This happens when the caret moves around or the selection is modified without any other action between changes. To preserve all caret changes as separate actions, you can use the preserveCaretChanges option:

let recorder = new Recorder(source, {preserveCaretChanges: true});

You can also provide options when calling start():

recorder.start({preserveCaretChanges: true});

Constructor: new Recorder(editor [, options])

Options:

Option Default Description
preserveCaretChanges false If true, will not coalesce consecutive caret position changes
pauseThreshold 2000 The delay (in ms) between consecutive actions that will cause a pause action to be inserted. Use 0 or false to disable pause actions entirely.
pauses undefined Set to "ignore" to not record pauses entirely.
pauseCap undefined Set to a number of milliseconds to cap pauses to that duration.
keys undefined Keystrokes to record, see How do I record custom keystrokes that don’t produce output?

To record custom keystrokes (that don’t result in output), you’d use the keys parameter with strings like "Ctrl + Shift + E". You can specify one or more keystrokes as an array. By default the keyup event is monitored. You can specify a different event by using an object instead of a string, e.g. {key: "Ctrl + Shift + E", event: "keydown"}.

Methods

Member Description
recorder.start() Start listening to edits
recorder.pause() Temporarily stop listening to edits

Properties and accessors

Member Description
recorder.actions The array of actions recorded so far.

Replayer class

The Replayer class allows you to run a single action or a sequence of actions on an <input>, <textarea>, or any replayer-compatible control.

Constructor: new Replayer(dest [, options])

dest is the same type as the first argument to the Recorder constructor. To replay actions on a single editor element, dest would be a reference to that element. To replay actions that span multiple editors, dest would be an object literal that maps ids to editor elements.

Options:

Option Default Description
delay 140 Delay between consecutive actions when runAll() is used
pauses "delay" What to do with pause actions? "delay" will just pause by that amount of time, "pause" will pause playback, "ignore" will discard them. You can also provide a function that decides which of these keywords to return based on the action specifics
animated_selection true Should selections be animated or happen at once?

Methods

Member Description
async replayer.runAll(actions) Run a sequence of actions. Returns a promise that resolves when all actions have ran or the replayer has been paused. If another sequence of actions is currently being played, it will stop it first, then replace the rest of its queue.
async replayer.queueAll(actions) Just like runAll() but instead of replacing the queue, it will add the actions to the existing queue.
async replayer.next() Runs the next action in the queue
async replayer.run([action]) Run a single action (except pauses, since this is pretty low-level and does not handle timing).
replayer.pause() Finish the action currently executing (if any), then pause.
async replayer.resume() Resumes playing the current queue.

Properties and Accessors

Member Description
recorder.queue Contains the actions that have been queued up for playing, but have not been played yet. Can also be set, and the array it is set to will be (shallowly) cloned.
recorder.paused true if the Replayer is paused or stopped, false if playing, undefined if the Replayer has not yet been used in any way.
recorder.played Array with actions that have already been played from the current queue. These actions have been removed from the queue.

FAQ

How do I record a demo from an arbitrary page, e.g. a live coded slide?

Drag this bookmarklet to your bookmarks toolbar: ⏺ Rety. Then, when you’re ready to record, press it. It will insert a button at the top right corner that you press to stop recording. When you stop recording, it will log the actions it recorded in the console, for your copying convenience. It will log them in two ways: both as an object, as well as a minified JSON serialization. Unfortunately, it does not yet allow customizing recording options.

What is the browser support?

Generally: all modern browsers. No IE11 or pre-Chromium Edge. More details:

  • Recorder makes heavy use of evt.inputType so it supports browsers that support that
  • Replayer makes heavy use of document.execCommand(), which limits Firefox support to Firefox 89+.
  • Both are written with well-supported modern ES features, such as private members. Since these generally have better support than evt.inputType, I did not bother transpiling.

What about unit tests?

I’m still trying to decide what's the best testing framework for mocking interactions with a textarea. If you have suggestions, please weigh in!

Where is the minified version?

This is currently a tiny codebase, so minifying is more trouble than it’s worth. No, it makes zero difference if you save one KB. If in the future the code grows enough that minifying adds value, there will be a minified version.

If you really can’t live with a non-minified asset, you can always use the generated version by jsdelivr.

I want to record actions from a custom element, not a built-in <input> or <textarea>

Recorder will work fine with any control that meets the following requirements:

  • Implements selectionStart and selectionEnd properties
  • Implements a value property
  • Emits input events with suitable inputType properties
  • Emits select events when the selection changes

I want to run actions on a custom element, not a built-in <input> or <textarea>

Replayer will work fine with any control that meets the following requirements:

  • Implements writable selectionStart and selectionEnd properties
  • Works well with document.execCommand() (the actions used are insertText, delete, forwardDelete, undo, redo)

How do I record custom keystrokes that don’t produce output?

When constructing Recorder objects, you can pass in a keys parameter, which is an array of custom keystrokes to record. These keystrokes can be specified as strings, like Ctrl + Enter or objects (like {key: "Ctrl + Enter", event: "keydown"}), if you also want to specify an event (other than keyup which is the default).

Recorder will then record key actions that match this keystroke. Replayer does not need to be taught about custom keystrokes, it replicates any key action it finds.

rety's People

Contributors

leaverou avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

rety's Issues

strange scrolling on demo action log

It's difficult to determine where to file this issue.
Looking at https://rety.verou.me/ there is a demo with an action log. I didn't do anything but scroll down, and that log looks funky. The scrollbar doesn't cover the entire element, and only the text decorations scroll. (so it's probably Prism)
(the yellow background is my chosen browser default, which is shown because it's not set in the page)

Edit: I forgot to mention this is in Firefox 102 on Ubuntu 20.04.

rety-action-log

Relative caret changes

It would help hand-editing a lot if caret changes could be recorded relative to the last caret action instead of as absolute text offsets.

These actions would have a "relative":true flag to distinguish from regular (absolute) caret actions.
We'd still need absolute caret actions for:

  • The first caret action in a script
  • The first caret action in each editor

Some notes:

  • Implementation will be a little tricky because caret actions need to be relative to the last caret action in that editor.
  • Resolving the relative offsets must not be incorrect when preserveCaretChanges is false.
  • The solution must not make recording one action O(N) i.e. you can't loop over the entire actions array in recorder#addAction()

Animated selection?

To more appropriately emulate human selection, perhaps select actions should be animated, i.e. one character at a time.
On the other hand, often selections are not done that way, e.g. words are often selected by double clicking, and lines by triple clicking. This indicates that maybe this information needs to be somehow preserved by Recorder.

The question is: is the complexity worth it or is the improvement in the end result too incremental?

Textarea does not scroll when needed

When replaying edits, the textarea doesn't scroll so that the part edited is always visible.

There are two ways to address this:

  1. In Replayer, scroll to part currently being edited. No way to do this in the web platform. For textareas that don't wrap we can approximate by number of line breaks before caret, but it's hacky and unreliable.
  2. Record scrolling as an action. Since sometimes the speaker scrolls to show something, this would allow rety to more closely replicate these types of demos. But what to record? We don't want to just record a px offset, since the actions may be replayed on a completely diifferent textarea with different styling. For the same reason, recording percentage offset also wouldn't be helpful. Maybe something related to font size? I.e. scroll offset divided by line height (for vertical scroll) or em width (for horizontal scroll). Also, which scrolls to record? If we record every time the scroll event fires, that would be very noisy.

Unit tests

Which testing framework shines at mocking user interactions with form controls?

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.