TT2-Play-Area
view release on metacpan or search on metacpan
lib/auto/TT2/Play/Area/public/codemirror/lib/codemirror.js view on Meta::CPAN
}
// This will be set to a {lineWise: bool, text: [string]} object, so
// that, when pasting, we know what kind of selections the copied
// text was made out of.
var lastCopied = null
function setLastCopied(newLastCopied) {
lastCopied = newLastCopied
}
function applyTextInput(cm, inserted, deleted, sel, origin) {
var doc = cm.doc
cm.display.shift = false
if (!sel) { sel = doc.sel }
var paste = cm.state.pasteIncoming || origin == "paste"
var textLines = splitLinesAuto(inserted), multiPaste = null
// When pasting N lines into N selections, insert one line per selection
if (paste && sel.ranges.length > 1) {
if (lastCopied && lastCopied.text.join("\n") == inserted) {
if (sel.ranges.length % lastCopied.text.length == 0) {
multiPaste = []
for (var i = 0; i < lastCopied.text.length; i++)
{ multiPaste.push(doc.splitLines(lastCopied.text[i])) }
}
} else if (textLines.length == sel.ranges.length && cm.options.pasteLinesPerSelection) {
multiPaste = map(textLines, function (l) { return [l]; })
}
}
var updateInput
// Normal behavior is to insert the new text into every selection
for (var i$1 = sel.ranges.length - 1; i$1 >= 0; i$1--) {
var range = sel.ranges[i$1]
var from = range.from(), to = range.to()
if (range.empty()) {
if (deleted && deleted > 0) // Handle deletion
{ from = Pos(from.line, from.ch - deleted) }
else if (cm.state.overwrite && !paste) // Handle overwrite
{ to = Pos(to.line, Math.min(getLine(doc, to.line).text.length, to.ch + lst(textLines).length)) }
else if (lastCopied && lastCopied.lineWise && lastCopied.text.join("\n") == inserted)
{ from = to = Pos(from.line, 0) }
}
updateInput = cm.curOp.updateInput
var changeEvent = {from: from, to: to, text: multiPaste ? multiPaste[i$1 % multiPaste.length] : textLines,
origin: origin || (paste ? "paste" : cm.state.cutIncoming ? "cut" : "+input")}
makeChange(cm.doc, changeEvent)
signalLater(cm, "inputRead", cm, changeEvent)
}
if (inserted && !paste)
{ triggerElectric(cm, inserted) }
ensureCursorVisible(cm)
cm.curOp.updateInput = updateInput
cm.curOp.typing = true
cm.state.pasteIncoming = cm.state.cutIncoming = false
}
function handlePaste(e, cm) {
var pasted = e.clipboardData && e.clipboardData.getData("Text")
if (pasted) {
e.preventDefault()
if (!cm.isReadOnly() && !cm.options.disableInput)
{ runInOp(cm, function () { return applyTextInput(cm, pasted, 0, null, "paste"); }) }
return true
}
}
function triggerElectric(cm, inserted) {
// When an 'electric' character is inserted, immediately trigger a reindent
if (!cm.options.electricChars || !cm.options.smartIndent) { return }
var sel = cm.doc.sel
for (var i = sel.ranges.length - 1; i >= 0; i--) {
var range = sel.ranges[i]
if (range.head.ch > 100 || (i && sel.ranges[i - 1].head.line == range.head.line)) { continue }
var mode = cm.getModeAt(range.head)
var indented = false
if (mode.electricChars) {
for (var j = 0; j < mode.electricChars.length; j++)
{ if (inserted.indexOf(mode.electricChars.charAt(j)) > -1) {
indented = indentLine(cm, range.head.line, "smart")
break
} }
} else if (mode.electricInput) {
if (mode.electricInput.test(getLine(cm.doc, range.head.line).text.slice(0, range.head.ch)))
{ indented = indentLine(cm, range.head.line, "smart") }
}
if (indented) { signalLater(cm, "electricInput", cm, range.head.line) }
}
}
function copyableRanges(cm) {
var text = [], ranges = []
for (var i = 0; i < cm.doc.sel.ranges.length; i++) {
var line = cm.doc.sel.ranges[i].head.line
var lineRange = {anchor: Pos(line, 0), head: Pos(line + 1, 0)}
ranges.push(lineRange)
text.push(cm.getRange(lineRange.anchor, lineRange.head))
}
return {text: text, ranges: ranges}
}
function disableBrowserMagic(field, spellcheck) {
field.setAttribute("autocorrect", "off")
field.setAttribute("autocapitalize", "off")
field.setAttribute("spellcheck", !!spellcheck)
}
function hiddenTextarea() {
var te = elt("textarea", null, null, "position: absolute; bottom: -1em; padding: 0; width: 1px; height: 1em; outline: none")
var div = elt("div", [te], null, "overflow: hidden; position: relative; width: 3px; height: 0px;")
// The textarea is kept positioned near the cursor to prevent the
// fact that it'll be scrolled into view on input from scrolling
// our fake cursor out of view. On webkit, when wrap=off, paste is
// very slow. So make the area wide instead.
if (webkit) { te.style.width = "1000px" }
else { te.setAttribute("wrap", "off") }
// If border: 0; -- iOS fails to open keyboard (issue #1287)
if (ios) { te.style.border = "1px solid black" }
lib/auto/TT2/Play/Area/public/codemirror/lib/codemirror.js view on Meta::CPAN
// CONTENTEDITABLE INPUT STYLE
var ContentEditableInput = function(cm) {
this.cm = cm
this.lastAnchorNode = this.lastAnchorOffset = this.lastFocusNode = this.lastFocusOffset = null
this.polling = new Delayed()
this.composing = null
this.gracePeriod = false
this.readDOMTimeout = null
};
ContentEditableInput.prototype.init = function (display) {
var this$1 = this;
var input = this, cm = input.cm
var div = input.div = display.lineDiv
disableBrowserMagic(div, cm.options.spellcheck)
on(div, "paste", function (e) {
if (signalDOMEvent(cm, e) || handlePaste(e, cm)) { return }
// IE doesn't fire input events, so we schedule a read for the pasted content in this way
if (ie_version <= 11) { setTimeout(operation(cm, function () { return this$1.updateFromDOM(); }), 20) }
})
on(div, "compositionstart", function (e) {
this$1.composing = {data: e.data, done: false}
})
on(div, "compositionupdate", function (e) {
if (!this$1.composing) { this$1.composing = {data: e.data, done: false} }
})
on(div, "compositionend", function (e) {
if (this$1.composing) {
if (e.data != this$1.composing.data) { this$1.readFromDOMSoon() }
this$1.composing.done = true
}
})
on(div, "touchstart", function () { return input.forceCompositionEnd(); })
on(div, "input", function () {
if (!this$1.composing) { this$1.readFromDOMSoon() }
})
function onCopyCut(e) {
if (signalDOMEvent(cm, e)) { return }
if (cm.somethingSelected()) {
setLastCopied({lineWise: false, text: cm.getSelections()})
if (e.type == "cut") { cm.replaceSelection("", null, "cut") }
} else if (!cm.options.lineWiseCopyCut) {
return
} else {
var ranges = copyableRanges(cm)
setLastCopied({lineWise: true, text: ranges.text})
if (e.type == "cut") {
cm.operation(function () {
cm.setSelections(ranges.ranges, 0, sel_dontScroll)
cm.replaceSelection("", null, "cut")
})
}
}
if (e.clipboardData) {
e.clipboardData.clearData()
var content = lastCopied.text.join("\n")
// iOS exposes the clipboard API, but seems to discard content inserted into it
e.clipboardData.setData("Text", content)
if (e.clipboardData.getData("Text") == content) {
e.preventDefault()
return
}
}
// Old-fashioned briefly-focus-a-textarea hack
var kludge = hiddenTextarea(), te = kludge.firstChild
cm.display.lineSpace.insertBefore(kludge, cm.display.lineSpace.firstChild)
te.value = lastCopied.text.join("\n")
var hadFocus = document.activeElement
selectInput(te)
setTimeout(function () {
cm.display.lineSpace.removeChild(kludge)
hadFocus.focus()
if (hadFocus == div) { input.showPrimarySelection() }
}, 50)
}
on(div, "copy", onCopyCut)
on(div, "cut", onCopyCut)
};
ContentEditableInput.prototype.prepareSelection = function () {
var result = prepareSelection(this.cm, false)
result.focus = this.cm.state.focused
return result
};
ContentEditableInput.prototype.showSelection = function (info, takeFocus) {
if (!info || !this.cm.display.view.length) { return }
if (info.focus || takeFocus) { this.showPrimarySelection() }
this.showMultipleSelections(info)
};
ContentEditableInput.prototype.showPrimarySelection = function () {
var sel = window.getSelection(), cm = this.cm, prim = cm.doc.sel.primary()
var from = prim.from(), to = prim.to()
if (cm.display.viewTo == cm.display.viewFrom || from.line >= cm.display.viewTo || to.line < cm.display.viewFrom) {
sel.removeAllRanges()
return
}
var curAnchor = domToPos(cm, sel.anchorNode, sel.anchorOffset)
var curFocus = domToPos(cm, sel.focusNode, sel.focusOffset)
if (curAnchor && !curAnchor.bad && curFocus && !curFocus.bad &&
cmp(minPos(curAnchor, curFocus), from) == 0 &&
cmp(maxPos(curAnchor, curFocus), to) == 0)
{ return }
var view = cm.display.view
var start = (from.line >= cm.display.viewFrom && posToDOM(cm, from)) ||
{node: view[0].measure.map[2], offset: 0}
var end = to.line < cm.display.viewTo && posToDOM(cm, to)
if (!end) {
var measure = view[view.length - 1].measure
var map = measure.maps ? measure.maps[measure.maps.length - 1] : measure.map
end = {node: map[map.length - 1], offset: map[map.length - 2] - map[map.length - 3]}
}
if (!start || !end) {
sel.removeAllRanges()
( run in 3.816 seconds using v1.01-cache-2.11-cpan-2398b32b56e )