Skip to content

Instantly share code, notes, and snippets.

View marijnh's full-sized avatar

Marijn Haverbeke marijnh

View GitHub Profile
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.acorn = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
// The main exported interface (under `self.acorn` when in the
// browser) is a `parse` function that takes a code string and
// returns an abstract syntax tree as specified by
@marijnh
marijnh / gist:3658e11554c3f1d891f3
Created May 22, 2015 08:54
Synchronization / collaborative editing algorithm
This is a collaborative editing protocol I came up with. Like OT, it
is change-based, not diff-based. But it is much easier to verify that
clients end up converging, because they all end up applying the
modifications in the same order (so that they _have to converge_, if
they are using the same algorithm).
Clients start with same version + version number
When a client makes a change, that is locally applied and a description
{
"!name": "node_modules/cocos2d/src/global.js",
"!define": {
"String.prototype._observingFunctions.<i>": {
"!span": "4511[148:60]-4552[148:101]"
},
"util.each.!1": {
"!type": "fn(mod: string|?, i: number)",
"!span": "9868[339:27]-10050[342:9]"
},
@marijnh
marijnh / cocos.json
Created July 25, 2015 21:35
cocos.json
{
"!name": "node_modules/cocos2d/src/global.js",
"!define": {
"String.prototype._observingFunctions.<i>": {
"!span": "4511[148:60]-4552[148:101]"
},
"util.each.!1": {
"!type": "fn(mod: string|?, i: number)",
"!span": "9868[339:27]-10050[342:9]"
},
<!doctype html>
<title>CodeMirror: merge view demo</title>
<meta charset="utf-8"/>
<link rel=stylesheet href="../doc/docs.css">
<link rel=stylesheet href="../lib/codemirror.css">
<link rel=stylesheet href="../addon/merge/merge.css">
<script src="//cdnjs.cloudflare.com/ajax/libs/require.js/2.1.14/require.min.js"></script>
<style>
@marijnh
marijnh / highlight.js
Last active December 23, 2023 08:08
Word highlighting in ProseMirror
import {Pos} from "../src/model"
function rangeFromTransform(tr) {
let from, to
for (let i = 0; i < tr.steps.length; i++) {
let step = tr.steps[i], map = tr.maps[i]
let stepFrom = map.map(step.from || step.pos, -1).pos
let stepTo = map.map(step.to || step.pos, 1).pos
from = from ? map.map(from, -1).pos.min(stepFrom) : stepFrom
to = to ? map.map(to, 1).pos.max(stepTo) : stepTo
@marijnh
marijnh / test.html
Created April 16, 2016 20:02
Mobile Safari keyboard issue
<!doctype html>
<div contenteditable=true style="border: 1px solid black">Foo bar</div>
<p>Put the cursor at the start of the editable content above, so
that the keyboard is in uppercase mode. Then press <button>this
button</button> to move the cursor to the end of 'foo'. Note that the
keyboard stays in uppercase mode althrough lowercase would be
appropriate for the new context.</p>
@marijnh
marijnh / changedRanges.js
Created August 17, 2016 14:11
Compute changed range from ProseMirror steps
function changedRanges(history, group) {
let ranges = []
history.forEach((step, i) => {
let map = step.posMap()
ranges = ranges.map(range => {
let from = map.map(range.from, 1), to = Math.max(from, map.map(range.to, -1))
return {from, to}
})
if (group.indexOf(i) > -1) {
let newRanges = []
@marijnh
marijnh / checkanchors.js
Created October 21, 2016 10:23
Check a page for dangling links to local anchors
let links = document.querySelectorAll("a[href]")
let base = /^[^#]*/.exec(document.location)[0] + "#"
for (let i = 0; i < links.length; i++) {
let link = links[i], anchor
if (link.href.indexOf(base) == 0 && !document.getElementById(anchor = decodeURIComponent(link.href.slice(base.length))))
console.log("Missing anchor: " + anchor)
}
@marijnh
marijnh / node-view.js
Created October 28, 2016 15:15
Demo of a ProseMirror editor that uses a nested code editor
const {EditorState, Selection} = require("prosemirror-state")
const {MenuBarEditorView} = require("prosemirror-menu")
const {DOMParser, DOMSerializer, Schema} = require("prosemirror-model")
const {schema: baseSchema} = require("prosemirror-schema-basic")
const {exampleSetup} = require("prosemirror-example-setup")
const {keymap} = require("prosemirror-keymap")
const CodeMirror = require("codemirror")
require("codemirror/mode/javascript/javascript")
let view, menuView, schema = new Schema({