mirror of
https://github.com/fofolee/uTools-quickcommand.git
synced 2025-12-18 18:04:28 +08:00
v0.0.1
This commit is contained in:
90
codemirror/src/util/StringStream.js
Normal file
90
codemirror/src/util/StringStream.js
Normal file
@@ -0,0 +1,90 @@
|
||||
import { countColumn } from "./misc.js"
|
||||
|
||||
// STRING STREAM
|
||||
|
||||
// Fed to the mode parsers, provides helper functions to make
|
||||
// parsers more succinct.
|
||||
|
||||
class StringStream {
|
||||
constructor(string, tabSize, lineOracle) {
|
||||
this.pos = this.start = 0
|
||||
this.string = string
|
||||
this.tabSize = tabSize || 8
|
||||
this.lastColumnPos = this.lastColumnValue = 0
|
||||
this.lineStart = 0
|
||||
this.lineOracle = lineOracle
|
||||
}
|
||||
|
||||
eol() {return this.pos >= this.string.length}
|
||||
sol() {return this.pos == this.lineStart}
|
||||
peek() {return this.string.charAt(this.pos) || undefined}
|
||||
next() {
|
||||
if (this.pos < this.string.length)
|
||||
return this.string.charAt(this.pos++)
|
||||
}
|
||||
eat(match) {
|
||||
let ch = this.string.charAt(this.pos)
|
||||
let ok
|
||||
if (typeof match == "string") ok = ch == match
|
||||
else ok = ch && (match.test ? match.test(ch) : match(ch))
|
||||
if (ok) {++this.pos; return ch}
|
||||
}
|
||||
eatWhile(match) {
|
||||
let start = this.pos
|
||||
while (this.eat(match)){}
|
||||
return this.pos > start
|
||||
}
|
||||
eatSpace() {
|
||||
let start = this.pos
|
||||
while (/[\s\u00a0]/.test(this.string.charAt(this.pos))) ++this.pos
|
||||
return this.pos > start
|
||||
}
|
||||
skipToEnd() {this.pos = this.string.length}
|
||||
skipTo(ch) {
|
||||
let found = this.string.indexOf(ch, this.pos)
|
||||
if (found > -1) {this.pos = found; return true}
|
||||
}
|
||||
backUp(n) {this.pos -= n}
|
||||
column() {
|
||||
if (this.lastColumnPos < this.start) {
|
||||
this.lastColumnValue = countColumn(this.string, this.start, this.tabSize, this.lastColumnPos, this.lastColumnValue)
|
||||
this.lastColumnPos = this.start
|
||||
}
|
||||
return this.lastColumnValue - (this.lineStart ? countColumn(this.string, this.lineStart, this.tabSize) : 0)
|
||||
}
|
||||
indentation() {
|
||||
return countColumn(this.string, null, this.tabSize) -
|
||||
(this.lineStart ? countColumn(this.string, this.lineStart, this.tabSize) : 0)
|
||||
}
|
||||
match(pattern, consume, caseInsensitive) {
|
||||
if (typeof pattern == "string") {
|
||||
let cased = str => caseInsensitive ? str.toLowerCase() : str
|
||||
let substr = this.string.substr(this.pos, pattern.length)
|
||||
if (cased(substr) == cased(pattern)) {
|
||||
if (consume !== false) this.pos += pattern.length
|
||||
return true
|
||||
}
|
||||
} else {
|
||||
let match = this.string.slice(this.pos).match(pattern)
|
||||
if (match && match.index > 0) return null
|
||||
if (match && consume !== false) this.pos += match[0].length
|
||||
return match
|
||||
}
|
||||
}
|
||||
current(){return this.string.slice(this.start, this.pos)}
|
||||
hideFirstChars(n, inner) {
|
||||
this.lineStart += n
|
||||
try { return inner() }
|
||||
finally { this.lineStart -= n }
|
||||
}
|
||||
lookAhead(n) {
|
||||
let oracle = this.lineOracle
|
||||
return oracle && oracle.lookAhead(n)
|
||||
}
|
||||
baseToken() {
|
||||
let oracle = this.lineOracle
|
||||
return oracle && oracle.baseToken(this.pos)
|
||||
}
|
||||
}
|
||||
|
||||
export default StringStream
|
||||
214
codemirror/src/util/bidi.js
Normal file
214
codemirror/src/util/bidi.js
Normal file
@@ -0,0 +1,214 @@
|
||||
import { lst } from "./misc.js"
|
||||
|
||||
// BIDI HELPERS
|
||||
|
||||
export function iterateBidiSections(order, from, to, f) {
|
||||
if (!order) return f(from, to, "ltr", 0)
|
||||
let found = false
|
||||
for (let i = 0; i < order.length; ++i) {
|
||||
let part = order[i]
|
||||
if (part.from < to && part.to > from || from == to && part.to == from) {
|
||||
f(Math.max(part.from, from), Math.min(part.to, to), part.level == 1 ? "rtl" : "ltr", i)
|
||||
found = true
|
||||
}
|
||||
}
|
||||
if (!found) f(from, to, "ltr")
|
||||
}
|
||||
|
||||
export let bidiOther = null
|
||||
export function getBidiPartAt(order, ch, sticky) {
|
||||
let found
|
||||
bidiOther = null
|
||||
for (let i = 0; i < order.length; ++i) {
|
||||
let cur = order[i]
|
||||
if (cur.from < ch && cur.to > ch) return i
|
||||
if (cur.to == ch) {
|
||||
if (cur.from != cur.to && sticky == "before") found = i
|
||||
else bidiOther = i
|
||||
}
|
||||
if (cur.from == ch) {
|
||||
if (cur.from != cur.to && sticky != "before") found = i
|
||||
else bidiOther = i
|
||||
}
|
||||
}
|
||||
return found != null ? found : bidiOther
|
||||
}
|
||||
|
||||
// Bidirectional ordering algorithm
|
||||
// See http://unicode.org/reports/tr9/tr9-13.html for the algorithm
|
||||
// that this (partially) implements.
|
||||
|
||||
// One-char codes used for character types:
|
||||
// L (L): Left-to-Right
|
||||
// R (R): Right-to-Left
|
||||
// r (AL): Right-to-Left Arabic
|
||||
// 1 (EN): European Number
|
||||
// + (ES): European Number Separator
|
||||
// % (ET): European Number Terminator
|
||||
// n (AN): Arabic Number
|
||||
// , (CS): Common Number Separator
|
||||
// m (NSM): Non-Spacing Mark
|
||||
// b (BN): Boundary Neutral
|
||||
// s (B): Paragraph Separator
|
||||
// t (S): Segment Separator
|
||||
// w (WS): Whitespace
|
||||
// N (ON): Other Neutrals
|
||||
|
||||
// Returns null if characters are ordered as they appear
|
||||
// (left-to-right), or an array of sections ({from, to, level}
|
||||
// objects) in the order in which they occur visually.
|
||||
let bidiOrdering = (function() {
|
||||
// Character types for codepoints 0 to 0xff
|
||||
let lowTypes = "bbbbbbbbbtstwsbbbbbbbbbbbbbbssstwNN%%%NNNNNN,N,N1111111111NNNNNNNLLLLLLLLLLLLLLLLLLLLLLLLLLNNNNNNLLLLLLLLLLLLLLLLLLLLLLLLLLNNNNbbbbbbsbbbbbbbbbbbbbbbbbbbbbbbbbb,N%%%%NNNNLNNNNN%%11NLNNN1LNNNNNLLLLLLLLLLLLLLLLLLLLLLLNLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLN"
|
||||
// Character types for codepoints 0x600 to 0x6f9
|
||||
let arabicTypes = "nnnnnnNNr%%r,rNNmmmmmmmmmmmrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrmmmmmmmmmmmmmmmmmmmmmnnnnnnnnnn%nnrrrmrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrmmmmmmmnNmmmmmmrrmmNmmmmrr1111111111"
|
||||
function charType(code) {
|
||||
if (code <= 0xf7) return lowTypes.charAt(code)
|
||||
else if (0x590 <= code && code <= 0x5f4) return "R"
|
||||
else if (0x600 <= code && code <= 0x6f9) return arabicTypes.charAt(code - 0x600)
|
||||
else if (0x6ee <= code && code <= 0x8ac) return "r"
|
||||
else if (0x2000 <= code && code <= 0x200b) return "w"
|
||||
else if (code == 0x200c) return "b"
|
||||
else return "L"
|
||||
}
|
||||
|
||||
let bidiRE = /[\u0590-\u05f4\u0600-\u06ff\u0700-\u08ac]/
|
||||
let isNeutral = /[stwN]/, isStrong = /[LRr]/, countsAsLeft = /[Lb1n]/, countsAsNum = /[1n]/
|
||||
|
||||
function BidiSpan(level, from, to) {
|
||||
this.level = level
|
||||
this.from = from; this.to = to
|
||||
}
|
||||
|
||||
return function(str, direction) {
|
||||
let outerType = direction == "ltr" ? "L" : "R"
|
||||
|
||||
if (str.length == 0 || direction == "ltr" && !bidiRE.test(str)) return false
|
||||
let len = str.length, types = []
|
||||
for (let i = 0; i < len; ++i)
|
||||
types.push(charType(str.charCodeAt(i)))
|
||||
|
||||
// W1. Examine each non-spacing mark (NSM) in the level run, and
|
||||
// change the type of the NSM to the type of the previous
|
||||
// character. If the NSM is at the start of the level run, it will
|
||||
// get the type of sor.
|
||||
for (let i = 0, prev = outerType; i < len; ++i) {
|
||||
let type = types[i]
|
||||
if (type == "m") types[i] = prev
|
||||
else prev = type
|
||||
}
|
||||
|
||||
// W2. Search backwards from each instance of a European number
|
||||
// until the first strong type (R, L, AL, or sor) is found. If an
|
||||
// AL is found, change the type of the European number to Arabic
|
||||
// number.
|
||||
// W3. Change all ALs to R.
|
||||
for (let i = 0, cur = outerType; i < len; ++i) {
|
||||
let type = types[i]
|
||||
if (type == "1" && cur == "r") types[i] = "n"
|
||||
else if (isStrong.test(type)) { cur = type; if (type == "r") types[i] = "R" }
|
||||
}
|
||||
|
||||
// W4. A single European separator between two European numbers
|
||||
// changes to a European number. A single common separator between
|
||||
// two numbers of the same type changes to that type.
|
||||
for (let i = 1, prev = types[0]; i < len - 1; ++i) {
|
||||
let type = types[i]
|
||||
if (type == "+" && prev == "1" && types[i+1] == "1") types[i] = "1"
|
||||
else if (type == "," && prev == types[i+1] &&
|
||||
(prev == "1" || prev == "n")) types[i] = prev
|
||||
prev = type
|
||||
}
|
||||
|
||||
// W5. A sequence of European terminators adjacent to European
|
||||
// numbers changes to all European numbers.
|
||||
// W6. Otherwise, separators and terminators change to Other
|
||||
// Neutral.
|
||||
for (let i = 0; i < len; ++i) {
|
||||
let type = types[i]
|
||||
if (type == ",") types[i] = "N"
|
||||
else if (type == "%") {
|
||||
let end
|
||||
for (end = i + 1; end < len && types[end] == "%"; ++end) {}
|
||||
let replace = (i && types[i-1] == "!") || (end < len && types[end] == "1") ? "1" : "N"
|
||||
for (let j = i; j < end; ++j) types[j] = replace
|
||||
i = end - 1
|
||||
}
|
||||
}
|
||||
|
||||
// W7. Search backwards from each instance of a European number
|
||||
// until the first strong type (R, L, or sor) is found. If an L is
|
||||
// found, then change the type of the European number to L.
|
||||
for (let i = 0, cur = outerType; i < len; ++i) {
|
||||
let type = types[i]
|
||||
if (cur == "L" && type == "1") types[i] = "L"
|
||||
else if (isStrong.test(type)) cur = type
|
||||
}
|
||||
|
||||
// N1. A sequence of neutrals takes the direction of the
|
||||
// surrounding strong text if the text on both sides has the same
|
||||
// direction. European and Arabic numbers act as if they were R in
|
||||
// terms of their influence on neutrals. Start-of-level-run (sor)
|
||||
// and end-of-level-run (eor) are used at level run boundaries.
|
||||
// N2. Any remaining neutrals take the embedding direction.
|
||||
for (let i = 0; i < len; ++i) {
|
||||
if (isNeutral.test(types[i])) {
|
||||
let end
|
||||
for (end = i + 1; end < len && isNeutral.test(types[end]); ++end) {}
|
||||
let before = (i ? types[i-1] : outerType) == "L"
|
||||
let after = (end < len ? types[end] : outerType) == "L"
|
||||
let replace = before == after ? (before ? "L" : "R") : outerType
|
||||
for (let j = i; j < end; ++j) types[j] = replace
|
||||
i = end - 1
|
||||
}
|
||||
}
|
||||
|
||||
// Here we depart from the documented algorithm, in order to avoid
|
||||
// building up an actual levels array. Since there are only three
|
||||
// levels (0, 1, 2) in an implementation that doesn't take
|
||||
// explicit embedding into account, we can build up the order on
|
||||
// the fly, without following the level-based algorithm.
|
||||
let order = [], m
|
||||
for (let i = 0; i < len;) {
|
||||
if (countsAsLeft.test(types[i])) {
|
||||
let start = i
|
||||
for (++i; i < len && countsAsLeft.test(types[i]); ++i) {}
|
||||
order.push(new BidiSpan(0, start, i))
|
||||
} else {
|
||||
let pos = i, at = order.length
|
||||
for (++i; i < len && types[i] != "L"; ++i) {}
|
||||
for (let j = pos; j < i;) {
|
||||
if (countsAsNum.test(types[j])) {
|
||||
if (pos < j) order.splice(at, 0, new BidiSpan(1, pos, j))
|
||||
let nstart = j
|
||||
for (++j; j < i && countsAsNum.test(types[j]); ++j) {}
|
||||
order.splice(at, 0, new BidiSpan(2, nstart, j))
|
||||
pos = j
|
||||
} else ++j
|
||||
}
|
||||
if (pos < i) order.splice(at, 0, new BidiSpan(1, pos, i))
|
||||
}
|
||||
}
|
||||
if (direction == "ltr") {
|
||||
if (order[0].level == 1 && (m = str.match(/^\s+/))) {
|
||||
order[0].from = m[0].length
|
||||
order.unshift(new BidiSpan(0, 0, m[0].length))
|
||||
}
|
||||
if (lst(order).level == 1 && (m = str.match(/\s+$/))) {
|
||||
lst(order).to -= m[0].length
|
||||
order.push(new BidiSpan(0, len - m[0].length, len))
|
||||
}
|
||||
}
|
||||
|
||||
return direction == "rtl" ? order.reverse() : order
|
||||
}
|
||||
})()
|
||||
|
||||
// Get the bidi ordering for the given line (and cache it). Returns
|
||||
// false for lines that are fully left-to-right, and an array of
|
||||
// BidiSpan objects otherwise.
|
||||
export function getOrder(line, direction) {
|
||||
let order = line.order
|
||||
if (order == null) order = line.order = bidiOrdering(line.text, direction)
|
||||
return order
|
||||
}
|
||||
33
codemirror/src/util/browser.js
Normal file
33
codemirror/src/util/browser.js
Normal file
@@ -0,0 +1,33 @@
|
||||
// Kludges for bugs and behavior differences that can't be feature
|
||||
// detected are enabled based on userAgent etc sniffing.
|
||||
let userAgent = navigator.userAgent
|
||||
let platform = navigator.platform
|
||||
|
||||
export let gecko = /gecko\/\d/i.test(userAgent)
|
||||
let ie_upto10 = /MSIE \d/.test(userAgent)
|
||||
let ie_11up = /Trident\/(?:[7-9]|\d{2,})\..*rv:(\d+)/.exec(userAgent)
|
||||
let edge = /Edge\/(\d+)/.exec(userAgent)
|
||||
export let ie = ie_upto10 || ie_11up || edge
|
||||
export let ie_version = ie && (ie_upto10 ? document.documentMode || 6 : +(edge || ie_11up)[1])
|
||||
export let webkit = !edge && /WebKit\//.test(userAgent)
|
||||
let qtwebkit = webkit && /Qt\/\d+\.\d+/.test(userAgent)
|
||||
export let chrome = !edge && /Chrome\//.test(userAgent)
|
||||
export let presto = /Opera\//.test(userAgent)
|
||||
export let safari = /Apple Computer/.test(navigator.vendor)
|
||||
export let mac_geMountainLion = /Mac OS X 1\d\D([8-9]|\d\d)\D/.test(userAgent)
|
||||
export let phantom = /PhantomJS/.test(userAgent)
|
||||
|
||||
export let ios = !edge && /AppleWebKit/.test(userAgent) && /Mobile\/\w+/.test(userAgent)
|
||||
export let android = /Android/.test(userAgent)
|
||||
// This is woefully incomplete. Suggestions for alternative methods welcome.
|
||||
export let mobile = ios || android || /webOS|BlackBerry|Opera Mini|Opera Mobi|IEMobile/i.test(userAgent)
|
||||
export let mac = ios || /Mac/.test(platform)
|
||||
export let chromeOS = /\bCrOS\b/.test(userAgent)
|
||||
export let windows = /win/i.test(platform)
|
||||
|
||||
let presto_version = presto && userAgent.match(/Version\/(\d*\.\d*)/)
|
||||
if (presto_version) presto_version = Number(presto_version[1])
|
||||
if (presto_version && presto_version >= 15) { presto = false; webkit = true }
|
||||
// Some browsers use the wrong event properties to signal cmd/ctrl on OS X
|
||||
export let flipCtrlCmd = mac && (qtwebkit || presto && (presto_version == null || presto_version < 12.11))
|
||||
export let captureRightClick = gecko || (ie && ie_version >= 9)
|
||||
97
codemirror/src/util/dom.js
Normal file
97
codemirror/src/util/dom.js
Normal file
@@ -0,0 +1,97 @@
|
||||
import { ie, ios } from "./browser.js"
|
||||
|
||||
export function classTest(cls) { return new RegExp("(^|\\s)" + cls + "(?:$|\\s)\\s*") }
|
||||
|
||||
export let rmClass = function(node, cls) {
|
||||
let current = node.className
|
||||
let match = classTest(cls).exec(current)
|
||||
if (match) {
|
||||
let after = current.slice(match.index + match[0].length)
|
||||
node.className = current.slice(0, match.index) + (after ? match[1] + after : "")
|
||||
}
|
||||
}
|
||||
|
||||
export function removeChildren(e) {
|
||||
for (let count = e.childNodes.length; count > 0; --count)
|
||||
e.removeChild(e.firstChild)
|
||||
return e
|
||||
}
|
||||
|
||||
export function removeChildrenAndAdd(parent, e) {
|
||||
return removeChildren(parent).appendChild(e)
|
||||
}
|
||||
|
||||
export function elt(tag, content, className, style) {
|
||||
let e = document.createElement(tag)
|
||||
if (className) e.className = className
|
||||
if (style) e.style.cssText = style
|
||||
if (typeof content == "string") e.appendChild(document.createTextNode(content))
|
||||
else if (content) for (let i = 0; i < content.length; ++i) e.appendChild(content[i])
|
||||
return e
|
||||
}
|
||||
// wrapper for elt, which removes the elt from the accessibility tree
|
||||
export function eltP(tag, content, className, style) {
|
||||
let e = elt(tag, content, className, style)
|
||||
e.setAttribute("role", "presentation")
|
||||
return e
|
||||
}
|
||||
|
||||
export let range
|
||||
if (document.createRange) range = function(node, start, end, endNode) {
|
||||
let r = document.createRange()
|
||||
r.setEnd(endNode || node, end)
|
||||
r.setStart(node, start)
|
||||
return r
|
||||
}
|
||||
else range = function(node, start, end) {
|
||||
let r = document.body.createTextRange()
|
||||
try { r.moveToElementText(node.parentNode) }
|
||||
catch(e) { return r }
|
||||
r.collapse(true)
|
||||
r.moveEnd("character", end)
|
||||
r.moveStart("character", start)
|
||||
return r
|
||||
}
|
||||
|
||||
export function contains(parent, child) {
|
||||
if (child.nodeType == 3) // Android browser always returns false when child is a textnode
|
||||
child = child.parentNode
|
||||
if (parent.contains)
|
||||
return parent.contains(child)
|
||||
do {
|
||||
if (child.nodeType == 11) child = child.host
|
||||
if (child == parent) return true
|
||||
} while (child = child.parentNode)
|
||||
}
|
||||
|
||||
export function activeElt() {
|
||||
// IE and Edge may throw an "Unspecified Error" when accessing document.activeElement.
|
||||
// IE < 10 will throw when accessed while the page is loading or in an iframe.
|
||||
// IE > 9 and Edge will throw when accessed in an iframe if document.body is unavailable.
|
||||
let activeElement
|
||||
try {
|
||||
activeElement = document.activeElement
|
||||
} catch(e) {
|
||||
activeElement = document.body || null
|
||||
}
|
||||
while (activeElement && activeElement.shadowRoot && activeElement.shadowRoot.activeElement)
|
||||
activeElement = activeElement.shadowRoot.activeElement
|
||||
return activeElement
|
||||
}
|
||||
|
||||
export function addClass(node, cls) {
|
||||
let current = node.className
|
||||
if (!classTest(cls).test(current)) node.className += (current ? " " : "") + cls
|
||||
}
|
||||
export function joinClasses(a, b) {
|
||||
let as = a.split(" ")
|
||||
for (let i = 0; i < as.length; i++)
|
||||
if (as[i] && !classTest(as[i]).test(b)) b += " " + as[i]
|
||||
return b
|
||||
}
|
||||
|
||||
export let selectInput = function(node) { node.select() }
|
||||
if (ios) // Mobile Safari apparently has a bug where select() is broken.
|
||||
selectInput = function(node) { node.selectionStart = 0; node.selectionEnd = node.value.length }
|
||||
else if (ie) // Suppress mysterious IE10 errors
|
||||
selectInput = function(node) { try { node.select() } catch(_e) {} }
|
||||
103
codemirror/src/util/event.js
Normal file
103
codemirror/src/util/event.js
Normal file
@@ -0,0 +1,103 @@
|
||||
import { mac } from "./browser.js"
|
||||
import { indexOf } from "./misc.js"
|
||||
|
||||
// EVENT HANDLING
|
||||
|
||||
// Lightweight event framework. on/off also work on DOM nodes,
|
||||
// registering native DOM handlers.
|
||||
|
||||
const noHandlers = []
|
||||
|
||||
export let on = function(emitter, type, f) {
|
||||
if (emitter.addEventListener) {
|
||||
emitter.addEventListener(type, f, false)
|
||||
} else if (emitter.attachEvent) {
|
||||
emitter.attachEvent("on" + type, f)
|
||||
} else {
|
||||
let map = emitter._handlers || (emitter._handlers = {})
|
||||
map[type] = (map[type] || noHandlers).concat(f)
|
||||
}
|
||||
}
|
||||
|
||||
export function getHandlers(emitter, type) {
|
||||
return emitter._handlers && emitter._handlers[type] || noHandlers
|
||||
}
|
||||
|
||||
export function off(emitter, type, f) {
|
||||
if (emitter.removeEventListener) {
|
||||
emitter.removeEventListener(type, f, false)
|
||||
} else if (emitter.detachEvent) {
|
||||
emitter.detachEvent("on" + type, f)
|
||||
} else {
|
||||
let map = emitter._handlers, arr = map && map[type]
|
||||
if (arr) {
|
||||
let index = indexOf(arr, f)
|
||||
if (index > -1)
|
||||
map[type] = arr.slice(0, index).concat(arr.slice(index + 1))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function signal(emitter, type /*, values...*/) {
|
||||
let handlers = getHandlers(emitter, type)
|
||||
if (!handlers.length) return
|
||||
let args = Array.prototype.slice.call(arguments, 2)
|
||||
for (let i = 0; i < handlers.length; ++i) handlers[i].apply(null, args)
|
||||
}
|
||||
|
||||
// The DOM events that CodeMirror handles can be overridden by
|
||||
// registering a (non-DOM) handler on the editor for the event name,
|
||||
// and preventDefault-ing the event in that handler.
|
||||
export function signalDOMEvent(cm, e, override) {
|
||||
if (typeof e == "string")
|
||||
e = {type: e, preventDefault: function() { this.defaultPrevented = true }}
|
||||
signal(cm, override || e.type, cm, e)
|
||||
return e_defaultPrevented(e) || e.codemirrorIgnore
|
||||
}
|
||||
|
||||
export function signalCursorActivity(cm) {
|
||||
let arr = cm._handlers && cm._handlers.cursorActivity
|
||||
if (!arr) return
|
||||
let set = cm.curOp.cursorActivityHandlers || (cm.curOp.cursorActivityHandlers = [])
|
||||
for (let i = 0; i < arr.length; ++i) if (indexOf(set, arr[i]) == -1)
|
||||
set.push(arr[i])
|
||||
}
|
||||
|
||||
export function hasHandler(emitter, type) {
|
||||
return getHandlers(emitter, type).length > 0
|
||||
}
|
||||
|
||||
// Add on and off methods to a constructor's prototype, to make
|
||||
// registering events on such objects more convenient.
|
||||
export function eventMixin(ctor) {
|
||||
ctor.prototype.on = function(type, f) {on(this, type, f)}
|
||||
ctor.prototype.off = function(type, f) {off(this, type, f)}
|
||||
}
|
||||
|
||||
// Due to the fact that we still support jurassic IE versions, some
|
||||
// compatibility wrappers are needed.
|
||||
|
||||
export function e_preventDefault(e) {
|
||||
if (e.preventDefault) e.preventDefault()
|
||||
else e.returnValue = false
|
||||
}
|
||||
export function e_stopPropagation(e) {
|
||||
if (e.stopPropagation) e.stopPropagation()
|
||||
else e.cancelBubble = true
|
||||
}
|
||||
export function e_defaultPrevented(e) {
|
||||
return e.defaultPrevented != null ? e.defaultPrevented : e.returnValue == false
|
||||
}
|
||||
export function e_stop(e) {e_preventDefault(e); e_stopPropagation(e)}
|
||||
|
||||
export function e_target(e) {return e.target || e.srcElement}
|
||||
export function e_button(e) {
|
||||
let b = e.which
|
||||
if (b == null) {
|
||||
if (e.button & 1) b = 1
|
||||
else if (e.button & 2) b = 3
|
||||
else if (e.button & 4) b = 2
|
||||
}
|
||||
if (mac && e.ctrlKey && b == 1) b = 3
|
||||
return b
|
||||
}
|
||||
84
codemirror/src/util/feature_detection.js
Normal file
84
codemirror/src/util/feature_detection.js
Normal file
@@ -0,0 +1,84 @@
|
||||
import { elt, range, removeChildren, removeChildrenAndAdd } from "./dom.js"
|
||||
import { ie, ie_version } from "./browser.js"
|
||||
|
||||
// Detect drag-and-drop
|
||||
export let dragAndDrop = function() {
|
||||
// There is *some* kind of drag-and-drop support in IE6-8, but I
|
||||
// couldn't get it to work yet.
|
||||
if (ie && ie_version < 9) return false
|
||||
let div = elt('div')
|
||||
return "draggable" in div || "dragDrop" in div
|
||||
}()
|
||||
|
||||
let zwspSupported
|
||||
export function zeroWidthElement(measure) {
|
||||
if (zwspSupported == null) {
|
||||
let test = elt("span", "\u200b")
|
||||
removeChildrenAndAdd(measure, elt("span", [test, document.createTextNode("x")]))
|
||||
if (measure.firstChild.offsetHeight != 0)
|
||||
zwspSupported = test.offsetWidth <= 1 && test.offsetHeight > 2 && !(ie && ie_version < 8)
|
||||
}
|
||||
let node = zwspSupported ? elt("span", "\u200b") :
|
||||
elt("span", "\u00a0", null, "display: inline-block; width: 1px; margin-right: -1px")
|
||||
node.setAttribute("cm-text", "")
|
||||
return node
|
||||
}
|
||||
|
||||
// Feature-detect IE's crummy client rect reporting for bidi text
|
||||
let badBidiRects
|
||||
export function hasBadBidiRects(measure) {
|
||||
if (badBidiRects != null) return badBidiRects
|
||||
let txt = removeChildrenAndAdd(measure, document.createTextNode("A\u062eA"))
|
||||
let r0 = range(txt, 0, 1).getBoundingClientRect()
|
||||
let r1 = range(txt, 1, 2).getBoundingClientRect()
|
||||
removeChildren(measure)
|
||||
if (!r0 || r0.left == r0.right) return false // Safari returns null in some cases (#2780)
|
||||
return badBidiRects = (r1.right - r0.right < 3)
|
||||
}
|
||||
|
||||
// See if "".split is the broken IE version, if so, provide an
|
||||
// alternative way to split lines.
|
||||
export let splitLinesAuto = "\n\nb".split(/\n/).length != 3 ? string => {
|
||||
let pos = 0, result = [], l = string.length
|
||||
while (pos <= l) {
|
||||
let nl = string.indexOf("\n", pos)
|
||||
if (nl == -1) nl = string.length
|
||||
let line = string.slice(pos, string.charAt(nl - 1) == "\r" ? nl - 1 : nl)
|
||||
let rt = line.indexOf("\r")
|
||||
if (rt != -1) {
|
||||
result.push(line.slice(0, rt))
|
||||
pos += rt + 1
|
||||
} else {
|
||||
result.push(line)
|
||||
pos = nl + 1
|
||||
}
|
||||
}
|
||||
return result
|
||||
} : string => string.split(/\r\n?|\n/)
|
||||
|
||||
export let hasSelection = window.getSelection ? te => {
|
||||
try { return te.selectionStart != te.selectionEnd }
|
||||
catch(e) { return false }
|
||||
} : te => {
|
||||
let range
|
||||
try {range = te.ownerDocument.selection.createRange()}
|
||||
catch(e) {}
|
||||
if (!range || range.parentElement() != te) return false
|
||||
return range.compareEndPoints("StartToEnd", range) != 0
|
||||
}
|
||||
|
||||
export let hasCopyEvent = (() => {
|
||||
let e = elt("div")
|
||||
if ("oncopy" in e) return true
|
||||
e.setAttribute("oncopy", "return;")
|
||||
return typeof e.oncopy == "function"
|
||||
})()
|
||||
|
||||
let badZoomedRects = null
|
||||
export function hasBadZoomedRects(measure) {
|
||||
if (badZoomedRects != null) return badZoomedRects
|
||||
let node = removeChildrenAndAdd(measure, elt("span", "x"))
|
||||
let normal = node.getBoundingClientRect()
|
||||
let fromRange = range(node, 0, 1).getBoundingClientRect()
|
||||
return badZoomedRects = Math.abs(normal.left - fromRange.left) > 1
|
||||
}
|
||||
150
codemirror/src/util/misc.js
Normal file
150
codemirror/src/util/misc.js
Normal file
@@ -0,0 +1,150 @@
|
||||
export function bind(f) {
|
||||
let args = Array.prototype.slice.call(arguments, 1)
|
||||
return function(){return f.apply(null, args)}
|
||||
}
|
||||
|
||||
export function copyObj(obj, target, overwrite) {
|
||||
if (!target) target = {}
|
||||
for (let prop in obj)
|
||||
if (obj.hasOwnProperty(prop) && (overwrite !== false || !target.hasOwnProperty(prop)))
|
||||
target[prop] = obj[prop]
|
||||
return target
|
||||
}
|
||||
|
||||
// Counts the column offset in a string, taking tabs into account.
|
||||
// Used mostly to find indentation.
|
||||
export function countColumn(string, end, tabSize, startIndex, startValue) {
|
||||
if (end == null) {
|
||||
end = string.search(/[^\s\u00a0]/)
|
||||
if (end == -1) end = string.length
|
||||
}
|
||||
for (let i = startIndex || 0, n = startValue || 0;;) {
|
||||
let nextTab = string.indexOf("\t", i)
|
||||
if (nextTab < 0 || nextTab >= end)
|
||||
return n + (end - i)
|
||||
n += nextTab - i
|
||||
n += tabSize - (n % tabSize)
|
||||
i = nextTab + 1
|
||||
}
|
||||
}
|
||||
|
||||
export class Delayed {
|
||||
constructor() {this.id = null}
|
||||
set(ms, f) {
|
||||
clearTimeout(this.id)
|
||||
this.id = setTimeout(f, ms)
|
||||
}
|
||||
}
|
||||
|
||||
export function indexOf(array, elt) {
|
||||
for (let i = 0; i < array.length; ++i)
|
||||
if (array[i] == elt) return i
|
||||
return -1
|
||||
}
|
||||
|
||||
// Number of pixels added to scroller and sizer to hide scrollbar
|
||||
export let scrollerGap = 30
|
||||
|
||||
// Returned or thrown by various protocols to signal 'I'm not
|
||||
// handling this'.
|
||||
export let Pass = {toString: function(){return "CodeMirror.Pass"}}
|
||||
|
||||
// Reused option objects for setSelection & friends
|
||||
export let sel_dontScroll = {scroll: false}, sel_mouse = {origin: "*mouse"}, sel_move = {origin: "+move"}
|
||||
|
||||
// The inverse of countColumn -- find the offset that corresponds to
|
||||
// a particular column.
|
||||
export function findColumn(string, goal, tabSize) {
|
||||
for (let pos = 0, col = 0;;) {
|
||||
let nextTab = string.indexOf("\t", pos)
|
||||
if (nextTab == -1) nextTab = string.length
|
||||
let skipped = nextTab - pos
|
||||
if (nextTab == string.length || col + skipped >= goal)
|
||||
return pos + Math.min(skipped, goal - col)
|
||||
col += nextTab - pos
|
||||
col += tabSize - (col % tabSize)
|
||||
pos = nextTab + 1
|
||||
if (col >= goal) return pos
|
||||
}
|
||||
}
|
||||
|
||||
let spaceStrs = [""]
|
||||
export function spaceStr(n) {
|
||||
while (spaceStrs.length <= n)
|
||||
spaceStrs.push(lst(spaceStrs) + " ")
|
||||
return spaceStrs[n]
|
||||
}
|
||||
|
||||
export function lst(arr) { return arr[arr.length-1] }
|
||||
|
||||
export function map(array, f) {
|
||||
let out = []
|
||||
for (let i = 0; i < array.length; i++) out[i] = f(array[i], i)
|
||||
return out
|
||||
}
|
||||
|
||||
export function insertSorted(array, value, score) {
|
||||
let pos = 0, priority = score(value)
|
||||
while (pos < array.length && score(array[pos]) <= priority) pos++
|
||||
array.splice(pos, 0, value)
|
||||
}
|
||||
|
||||
function nothing() {}
|
||||
|
||||
export function createObj(base, props) {
|
||||
let inst
|
||||
if (Object.create) {
|
||||
inst = Object.create(base)
|
||||
} else {
|
||||
nothing.prototype = base
|
||||
inst = new nothing()
|
||||
}
|
||||
if (props) copyObj(props, inst)
|
||||
return inst
|
||||
}
|
||||
|
||||
let nonASCIISingleCaseWordChar = /[\u00df\u0587\u0590-\u05f4\u0600-\u06ff\u3040-\u309f\u30a0-\u30ff\u3400-\u4db5\u4e00-\u9fcc\uac00-\ud7af]/
|
||||
export function isWordCharBasic(ch) {
|
||||
return /\w/.test(ch) || ch > "\x80" &&
|
||||
(ch.toUpperCase() != ch.toLowerCase() || nonASCIISingleCaseWordChar.test(ch))
|
||||
}
|
||||
export function isWordChar(ch, helper) {
|
||||
if (!helper) return isWordCharBasic(ch)
|
||||
if (helper.source.indexOf("\\w") > -1 && isWordCharBasic(ch)) return true
|
||||
return helper.test(ch)
|
||||
}
|
||||
|
||||
export function isEmpty(obj) {
|
||||
for (let n in obj) if (obj.hasOwnProperty(n) && obj[n]) return false
|
||||
return true
|
||||
}
|
||||
|
||||
// Extending unicode characters. A series of a non-extending char +
|
||||
// any number of extending chars is treated as a single unit as far
|
||||
// as editing and measuring is concerned. This is not fully correct,
|
||||
// since some scripts/fonts/browsers also treat other configurations
|
||||
// of code points as a group.
|
||||
let extendingChars = /[\u0300-\u036f\u0483-\u0489\u0591-\u05bd\u05bf\u05c1\u05c2\u05c4\u05c5\u05c7\u0610-\u061a\u064b-\u065e\u0670\u06d6-\u06dc\u06de-\u06e4\u06e7\u06e8\u06ea-\u06ed\u0711\u0730-\u074a\u07a6-\u07b0\u07eb-\u07f3\u0816-\u0819\u081b-\u0823\u0825-\u0827\u0829-\u082d\u0900-\u0902\u093c\u0941-\u0948\u094d\u0951-\u0955\u0962\u0963\u0981\u09bc\u09be\u09c1-\u09c4\u09cd\u09d7\u09e2\u09e3\u0a01\u0a02\u0a3c\u0a41\u0a42\u0a47\u0a48\u0a4b-\u0a4d\u0a51\u0a70\u0a71\u0a75\u0a81\u0a82\u0abc\u0ac1-\u0ac5\u0ac7\u0ac8\u0acd\u0ae2\u0ae3\u0b01\u0b3c\u0b3e\u0b3f\u0b41-\u0b44\u0b4d\u0b56\u0b57\u0b62\u0b63\u0b82\u0bbe\u0bc0\u0bcd\u0bd7\u0c3e-\u0c40\u0c46-\u0c48\u0c4a-\u0c4d\u0c55\u0c56\u0c62\u0c63\u0cbc\u0cbf\u0cc2\u0cc6\u0ccc\u0ccd\u0cd5\u0cd6\u0ce2\u0ce3\u0d3e\u0d41-\u0d44\u0d4d\u0d57\u0d62\u0d63\u0dca\u0dcf\u0dd2-\u0dd4\u0dd6\u0ddf\u0e31\u0e34-\u0e3a\u0e47-\u0e4e\u0eb1\u0eb4-\u0eb9\u0ebb\u0ebc\u0ec8-\u0ecd\u0f18\u0f19\u0f35\u0f37\u0f39\u0f71-\u0f7e\u0f80-\u0f84\u0f86\u0f87\u0f90-\u0f97\u0f99-\u0fbc\u0fc6\u102d-\u1030\u1032-\u1037\u1039\u103a\u103d\u103e\u1058\u1059\u105e-\u1060\u1071-\u1074\u1082\u1085\u1086\u108d\u109d\u135f\u1712-\u1714\u1732-\u1734\u1752\u1753\u1772\u1773\u17b7-\u17bd\u17c6\u17c9-\u17d3\u17dd\u180b-\u180d\u18a9\u1920-\u1922\u1927\u1928\u1932\u1939-\u193b\u1a17\u1a18\u1a56\u1a58-\u1a5e\u1a60\u1a62\u1a65-\u1a6c\u1a73-\u1a7c\u1a7f\u1b00-\u1b03\u1b34\u1b36-\u1b3a\u1b3c\u1b42\u1b6b-\u1b73\u1b80\u1b81\u1ba2-\u1ba5\u1ba8\u1ba9\u1c2c-\u1c33\u1c36\u1c37\u1cd0-\u1cd2\u1cd4-\u1ce0\u1ce2-\u1ce8\u1ced\u1dc0-\u1de6\u1dfd-\u1dff\u200c\u200d\u20d0-\u20f0\u2cef-\u2cf1\u2de0-\u2dff\u302a-\u302f\u3099\u309a\ua66f-\ua672\ua67c\ua67d\ua6f0\ua6f1\ua802\ua806\ua80b\ua825\ua826\ua8c4\ua8e0-\ua8f1\ua926-\ua92d\ua947-\ua951\ua980-\ua982\ua9b3\ua9b6-\ua9b9\ua9bc\uaa29-\uaa2e\uaa31\uaa32\uaa35\uaa36\uaa43\uaa4c\uaab0\uaab2-\uaab4\uaab7\uaab8\uaabe\uaabf\uaac1\uabe5\uabe8\uabed\udc00-\udfff\ufb1e\ufe00-\ufe0f\ufe20-\ufe26\uff9e\uff9f]/
|
||||
export function isExtendingChar(ch) { return ch.charCodeAt(0) >= 768 && extendingChars.test(ch) }
|
||||
|
||||
// Returns a number from the range [`0`; `str.length`] unless `pos` is outside that range.
|
||||
export function skipExtendingChars(str, pos, dir) {
|
||||
while ((dir < 0 ? pos > 0 : pos < str.length) && isExtendingChar(str.charAt(pos))) pos += dir
|
||||
return pos
|
||||
}
|
||||
|
||||
// Returns the value from the range [`from`; `to`] that satisfies
|
||||
// `pred` and is closest to `from`. Assumes that at least `to`
|
||||
// satisfies `pred`. Supports `from` being greater than `to`.
|
||||
export function findFirst(pred, from, to) {
|
||||
// At any point we are certain `to` satisfies `pred`, don't know
|
||||
// whether `from` does.
|
||||
let dir = from > to ? -1 : 1
|
||||
for (;;) {
|
||||
if (from == to) return from
|
||||
let midF = (from + to) / 2, mid = dir < 0 ? Math.ceil(midF) : Math.floor(midF)
|
||||
if (mid == from) return pred(mid) ? from : to
|
||||
if (pred(mid)) to = mid
|
||||
else from = mid + dir
|
||||
}
|
||||
}
|
||||
72
codemirror/src/util/operation_group.js
Normal file
72
codemirror/src/util/operation_group.js
Normal file
@@ -0,0 +1,72 @@
|
||||
import { getHandlers } from "./event.js"
|
||||
|
||||
let operationGroup = null
|
||||
|
||||
export function pushOperation(op) {
|
||||
if (operationGroup) {
|
||||
operationGroup.ops.push(op)
|
||||
} else {
|
||||
op.ownsGroup = operationGroup = {
|
||||
ops: [op],
|
||||
delayedCallbacks: []
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function fireCallbacksForOps(group) {
|
||||
// Calls delayed callbacks and cursorActivity handlers until no
|
||||
// new ones appear
|
||||
let callbacks = group.delayedCallbacks, i = 0
|
||||
do {
|
||||
for (; i < callbacks.length; i++)
|
||||
callbacks[i].call(null)
|
||||
for (let j = 0; j < group.ops.length; j++) {
|
||||
let op = group.ops[j]
|
||||
if (op.cursorActivityHandlers)
|
||||
while (op.cursorActivityCalled < op.cursorActivityHandlers.length)
|
||||
op.cursorActivityHandlers[op.cursorActivityCalled++].call(null, op.cm)
|
||||
}
|
||||
} while (i < callbacks.length)
|
||||
}
|
||||
|
||||
export function finishOperation(op, endCb) {
|
||||
let group = op.ownsGroup
|
||||
if (!group) return
|
||||
|
||||
try { fireCallbacksForOps(group) }
|
||||
finally {
|
||||
operationGroup = null
|
||||
endCb(group)
|
||||
}
|
||||
}
|
||||
|
||||
let orphanDelayedCallbacks = null
|
||||
|
||||
// Often, we want to signal events at a point where we are in the
|
||||
// middle of some work, but don't want the handler to start calling
|
||||
// other methods on the editor, which might be in an inconsistent
|
||||
// state or simply not expect any other events to happen.
|
||||
// signalLater looks whether there are any handlers, and schedules
|
||||
// them to be executed when the last operation ends, or, if no
|
||||
// operation is active, when a timeout fires.
|
||||
export function signalLater(emitter, type /*, values...*/) {
|
||||
let arr = getHandlers(emitter, type)
|
||||
if (!arr.length) return
|
||||
let args = Array.prototype.slice.call(arguments, 2), list
|
||||
if (operationGroup) {
|
||||
list = operationGroup.delayedCallbacks
|
||||
} else if (orphanDelayedCallbacks) {
|
||||
list = orphanDelayedCallbacks
|
||||
} else {
|
||||
list = orphanDelayedCallbacks = []
|
||||
setTimeout(fireOrphanDelayed, 0)
|
||||
}
|
||||
for (let i = 0; i < arr.length; ++i)
|
||||
list.push(() => arr[i].apply(null, args))
|
||||
}
|
||||
|
||||
function fireOrphanDelayed() {
|
||||
let delayed = orphanDelayedCallbacks
|
||||
orphanDelayedCallbacks = null
|
||||
for (let i = 0; i < delayed.length; ++i) delayed[i]()
|
||||
}
|
||||
Reference in New Issue
Block a user