diff options
author | Matthew Sotoudeh <matthewsot@outlook.com> | 2019-08-09 23:34:16 -0700 |
---|---|---|
committer | Matthew Sotoudeh <matthewsot@outlook.com> | 2019-08-09 23:34:16 -0700 |
commit | ce765b5647433b3505f5314174a85c3599c8876a (patch) | |
tree | 8b6f803db585df269f1a4feb2d4892b8cdf2b730 | |
parent | a1cf86c872c00faa92daa080abbdd2eba9b28ead (diff) |
Basic functionality working in modern versions of Chrome/Firefox again.
-rw-r--r-- | docs.js | 146 | ||||
-rw-r--r-- | editor-input.js | 71 | ||||
-rw-r--r-- | utils.js | 39 |
3 files changed, 103 insertions, 153 deletions
@@ -1,4 +1,4 @@ -var docs = docs || { //TODO: cleanup the duplication here + in utils.js +var docs = docs || { // TODO: cleanup the duplication here + in utils.js id: window.location.href.split("/document/d/")[1].split("/")[0], get name() { return $(".docs-title-input-label-inner").text().trim(); @@ -52,97 +52,10 @@ docs.getSelectionWithObserver = function (callback, defaultToParagraph, getRaw) }; docs.getSelection = docs.getSelectionWithObserver; -docs.runWithCreateKeyboard = function (strToRun, funcName, params) { - var toRun = "var docs = {'utils': {}};docs.utils.createKeyboardEvent = " + docs.utils.createKeyboardEvent.toString() + ";"; - - toRun += strToRun; - - toRun += funcName + '(' + params + ')'; - - docs.utils.runInPage(toRun.replace(/\r?\n|\r/g, " ")); -}; - -docs.insertText = function (toInsert) { - function doInsertText(toInsert) { - for (var i = 0; i < toInsert.length; i++) { - var key = toInsert[i]; - var specials = { " ": " ", "\t": "Tab" }; - if (specials[key] !== "undefined") { - key = specials[key]; - } - - var e = docs.utils.createKeyboardEvent("keypress", { - "key": key, - "charCode": toInsert.charCodeAt(i), - "keyCode": null, - "bubbles": true, - "cancelable": true, - "code": key, - "pageY": 7, - "which": toInsert.charCodeAt(i) - }); - - document.getElementsByClassName("docs-texteventtarget-iframe")[0].contentWindow.document.querySelector("[contenteditable=\"true\"]").dispatchEvent(e); - } - } - - if (docs.platform !== "userscript") { - docs.runWithCreateKeyboard(doInsertText.toString(), "doInsertText", "\"" + toInsert + "\""); - } else { - doInsertText(toInsert); - } -}; - -docs.lastBackspace = Date.now(); -docs.backspace = function (counts) { - function doBackspace(counts) { - var keyboardType = "keypress"; - if (navigator.vendor.toLowerCase().indexOf("google") !== -1) { keyboardType = "keydown"; } - - var e = docs.utils.createKeyboardEvent(keyboardType, { - "key": "Backspace", - "charCode": 8, - "keyCode": 8, - "bubbles": true, - "cancelable": true, - "code": "Backspace", - "pageY": 7, - "which": 8 - }); - - if (typeof counts === "undefined") counts = 1; - - for (var i = 0; i < counts; i++) { - document.getElementsByClassName("docs-texteventtarget-iframe")[0].contentWindow.document.querySelector("[contenteditable=\"true\"]").dispatchEvent(e); - } - } - - if (typeof counts === "undefined") counts = 1; - - var secondsSinceLastBackspace = (Date.now() - docs.lastBackspace) / 1000; - - if (counts === 1 && secondsSinceLastBackspace < 1) return; //just trust me here - - lastBackspace = Date.now(); - var secondsTimeout = secondsSinceLastBackspace < 1 ? (1 - secondsSinceLastBackspace) : 0; - - - var toRun = "var docs = {'utils': {}};docs.utils.createKeyboardEvent = " + docs.utils.createKeyboardEvent.toString() + ";"; - - toRun += doBackspace.toString(); - - toRun += 'doBackspace(' + counts + ')'; - - setTimeout(function () { - docs.utils.runInPage(toRun.replace(/\r?\n|\r/g, " ")); - }, (secondsTimeout * 1000)); - return; -}; - docs.colors = { - "gray": "#jfk-palette-cell-5 > div:nth-child(1)", - "black": "#jfk-palette-cell-0 > div:nth-child(1)", - "white": "#jfk-palette-cell-9 > div:nth-child(1)" + "gray": "#docs-material-colorpalette-cell-5 > div:nth-child(1)", + "black": "#docs-material-colorpalette-cell-0 > div:nth-child(1)", + "white": "#docs-material-colorpalette-cell-9 > div:nth-child(1)" }; docs.setColor = function (colorSelector) { @@ -200,33 +113,26 @@ docs.ctrlKeyShortcut = function (secondKey) { } }; +docs.toggleSuperscript = function () { + var el = document.getElementsByClassName("docs-texteventtarget-iframe")[0].contentDocument; + var data = {"keyCode": 190, "ctrlKey": true}; + key_event = new KeyboardEvent("keydown", data); + key_event.docs_plus_ = true; + el.dispatchEvent(key_event); +}; docs.toggleSubscript = function () { - var comma = { - "key": ",", - "charCode": 0, - "keyCode": 188, - "bubbles": true, - "cancelable": true, - "code": 188, - "pageY": 7, - "which": 188, - "ctrlKey": true - }; - docs.ctrlKeyShortcut(comma); + var el = document.getElementsByClassName("docs-texteventtarget-iframe")[0].contentDocument; + var data = {"keyCode": 188, "ctrlKey": true}; + key_event = new KeyboardEvent("keydown", data); + key_event.docs_plus_ = true; + el.dispatchEvent(key_event); }; docs.toggleBold = function () { - var b = { - "key": "b", - "charCode": 0, - "keyCode": 66, - "bubbles": true, - "cancelable": true, - "code": 66, - "pageY": 7, - "which": 66, - "ctrlKey": true - }; - docs.ctrlKeyShortcut(b); + var el = document.getElementsByClassName("docs-texteventtarget-iframe")[0].contentDocument; + var data = {"keyCode": 66, "ctrlKey": true}; + key_event = new KeyboardEvent("keydown", data); + key_event.docs_plus_ = true; + el.dispatchEvent(key_event); }; docs.getCurrentParagraphText = function(callback) { @@ -266,7 +172,7 @@ docs.getCurrentParagraphText = function(callback) { }); }); - docs.insertText(" "); + docs.keyboard.typeLetters(" "); }; docs.getUserCursor = function () { @@ -288,3 +194,11 @@ docs.getUserCursor = function () { console.log("Couldn't locate the cursor!"); return $(".kix-cursor").first(); }; + +docs.setCursorWidth = function(width) { + docs.getUserCursor().find(".kix-cursor-caret").css("border-width", width); +}; + +var el = document.getElementsByClassName("docs-texteventtarget-iframe")[0].contentWindow.document.querySelector("[contenteditable=\"true\"]"); +ev = new KeyboardEvent("keydown", {"code": 37}) +el.dispatchEvent(ev); diff --git a/editor-input.js b/editor-input.js index d13340a..e8b0076 100644 --- a/editor-input.js +++ b/editor-input.js @@ -1,4 +1,29 @@ -docs.keyboard = {}; +docs.keyboard = { + "blockedKeydown_": function (e) { + if (e.docs_plus_) { + return; + } + if (!docs.keyboard.blockedKeydown) { + return; + } + if (typeof e.key === "undefined") { + e.key = docs.utils.keyFromKeyCode(e.shiftKey, e.keyCode); + } + docs.keyboard.blockedKeydown(e); + }, + "unblockedKeydown_": function (e) { + if (e.docs_plus_) { + return; + } + if (!docs.keyboard.unblockedKeydown) { + return; + } + if (typeof e.key === "undefined") { + e.key = docs.utils.keyFromKeyCode(e.shiftKey, e.keyCode); + } + docs.keyboard.unblockedKeydown(e); + }, +}; docs.keyboard.startBlockingMouse = function () { $(".kix-appview-editor").on("mousedown click mouseup mousemove mouseover", docs.utils.stopEvent); @@ -13,17 +38,45 @@ docs.keyboard.stopBlockingMouse = function () { }; docs.keyboard.startBlockingKeyboard = function () { - $(".docs-texteventtarget-iframe").contents().find("[contenteditable=\"true\"]").on("keydown", docs.keyboard.handleKeyboard); + if (docs.keyboard.blockedKeydown) { + $(".docs-texteventtarget-iframe").contents().find("[contenteditable=\"true\"]").on("keydown", docs.keyboard.blockedKeydown_); + } + if (docs.keyboard.unblockedKeydown) { + $(".docs-texteventtarget-iframe").contents().find("[contenteditable=\"true\"]").off("keydown", docs.keyboard.unblockedKeydown_); + } }; docs.keyboard.stopBlockingKeyboard = function () { - $(".docs-texteventtarget-iframe").contents().find("[contenteditable=\"true\"]").off("keydown", docs.keyboard.handleKeyboard); + if (docs.keyboard.blockedKeydown) { + $(".docs-texteventtarget-iframe").contents().find("[contenteditable=\"true\"]").off("keydown", docs.keyboard.blockedKeydown_); + } + if (docs.keyboard.unblockedKeydown) { + $(".docs-texteventtarget-iframe").contents().find("[contenteditable=\"true\"]").on("keydown", docs.keyboard.unblockedKeydown_); + } }; -$(".docs-texteventtarget-iframe").contents().find("[contenteditable=\"true\"]").keydown(function (e) { - if (!docs.keyboard.handleKeydown) return; +$(".docs-texteventtarget-iframe").contents().find("[contenteditable=\"true\"]").on("keydown", docs.keyboard.unblockedKeydown_); + +docs.keyboard.pressKey = function (keyCode) { + var el = document.getElementsByClassName("docs-texteventtarget-iframe")[0].contentDocument; + var data = {"keyCode": keyCode}; + var is_command = (keyCode <= 46); + var key_event; + if (is_command) { + key_event = new KeyboardEvent("keydown", data); + } else { + key_event = new KeyboardEvent("keypress", data); + } + + key_event.docs_plus_ = true; + el.dispatchEvent(key_event); +}; - if (typeof e.key === "undefined") { - e.key = docs.utils.keyFromKeyCode(e.shiftKey, e.keyCode); +docs.keyboard.typeLetters = function (text) { + for (var i = 0; i < character.length; i++) { + docs.keyboard.pressKey(text.charCodeAt(i)); } - docs.keyboard.handleKeydown(e); -}); +}; + +docs.keyboard.backspace = function (counts) { + docs.keyboard.pressKey(8); +}; @@ -26,8 +26,8 @@ docs.utils.observe = function (el, config, callback, observeOnce) { docs.utils.keyFromKeyCode = function (shifted, keyCode) { var specialKeys = { 191: "/", 27: "Escape", 16: "Shift", 17: "Control", 18: "Alt", 8: "Backspace", 32: " ", 13: "Enter", 9: "Tab", 37: "ArrowLeft", 38: "ArrowUp", 39: "ArrowRight", 40: "ArrowDown" }; var shiftedSpecialKeys = { 191: "?" }; - if (typeof specialKeys[keyCode] !== "undefined") { - if (shifted && typeof shiftedSpecialKeys[keyCode] !== "undefined") { + if (keyCode in specialKeys) { + if (shifted && keyCode in shiftedSpecialKeys) { return shiftedSpecialKeys[keyCode]; } return specialKeys[keyCode]; @@ -36,8 +36,7 @@ docs.utils.keyFromKeyCode = function (shifted, keyCode) { if(!shifted) return c.toLowerCase(); var shifts = { "`": "~", "1": "!", "2": "@", "3": "#", "4": "$", "5": "%", "6": "^", "7": "&", "8": "*", "9": "(", "0": ")", "-": "_", "=": "+" }; - var foundShift = shifts[c]; - if (typeof foundShift === "undefined") { + if (!(c in shifts)) { return c.toUpperCase(); } else { return shifts[c]; @@ -45,31 +44,15 @@ docs.utils.keyFromKeyCode = function (shifted, keyCode) { } }; -docs.utils.createKeyboardEvent = function (type, info) { - var e = new KeyboardEvent(type, info); - if(e.keyCode == 0) { - /* http://jsbin.com/awenaq/3/edit?js,output */ - e = document.createEventObject ? - document.createEventObject() : document.createEvent("Events"); - - if(e.initEvent){ - e.initEvent(type, true, true); - } - - for (var prop in info) { - e[prop] = info[prop]; - } +docs.utils.codeFromKey = function(key) { + var specialKeys = { "/": 191, "Escape": 27, "Shift": 16, + "Control": 17, "Alt": 18, "Backspace": 8, " ": 32, "Enter": 13, + "Tab": 9, "ArrowLeft": 37, "ArrowUp": 38, "ArrowRight": 39, "ArrowDown": 40, + "Delete": 46 }; + if (key in specialKeys) { + return specialKeys[key]; } - - return e; -} - -docs.utils.runInPage = function (script) { - var th = document.body; - var s = document.createElement('script'); - s.setAttribute('type', 'text/javascript'); - s.innerHTML = script; - th.appendChild(s); + return key.charCodeAt(key); }; //Thanks! https://stackoverflow.com/questions/1740700/how-to-get-hex-color-value-rather-than-rgb-value |