Skip to content

Commit

Permalink
jscode enhance
Browse files Browse the repository at this point in the history
jscode enhance
  • Loading branch information
clark-cui authored and tiensonqin committed Sep 26, 2021
1 parent 40a5cec commit 01eb908
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 106 deletions.
32 changes: 16 additions & 16 deletions src/main/frontend/exif.js
@@ -1,41 +1,41 @@
// copied from https://stackoverflow.com/questions/7584794/accessing-jpeg-exif-rotation-data-in-javascript-on-the-client-side

function objectURLToBlob(url, callback) {
var http = new XMLHttpRequest();
const objectURLToBlob=(url, callback)=> {
const http = new XMLHttpRequest();
http.open("GET", url, true);
http.responseType = "blob";
http.onload = function(e) {
if (this.status == 200 || this.status === 0) {
callback(this.response);
http.onload = (e)=> {
if (http.status == 200 || http.status === 0) {
callback(http.response);
}
};
http.send();
}

export var getEXIFOrientation = function (img, callback) {
var reader = new FileReader();
export const getEXIFOrientation = (img, callback)=> {
const reader = new FileReader();
reader.onload = e => {
var view = new DataView(e.target.result)
const view = new DataView(e.target.result)

if (view.getUint16(0, false) !== 0xFFD8) {
return callback(-2)
}
var length = view.byteLength
var offset = 2
const length = view.byteLength
let offset = 2
while (offset < length) {
var marker = view.getUint16(offset, false)
const marker = view.getUint16(offset, false)
offset += 2
if (marker === 0xFFE1) {
if (view.getUint32(offset += 2, false) !== 0x45786966) {
return callback(-1)
}
var little = view.getUint16(offset += 6, false) === 0x4949
const little = view.getUint16(offset += 6, false) === 0x4949
offset += view.getUint32(offset + 4, little)
var tags = view.getUint16(offset, little)
const tags = view.getUint16(offset, little)
offset += 2
for (var i = 0; i < tags; i++) {
for (let i = 0; i < tags; i++) {
if (view.getUint16(offset + (i * 12), little) === 0x0112) {
var o = view.getUint16(offset + (i * 12) + 8, little);
const o = view.getUint16(offset + (i * 12) + 8, little);
return callback(o)
}
}
Expand All @@ -48,7 +48,7 @@ export var getEXIFOrientation = function (img, callback) {
return callback(-1)
};

objectURLToBlob(img.src, function (blob) {
objectURLToBlob(img.src, (blob)=> {
reader.readAsArrayBuffer(blob.slice(0, 65536));
});
}
22 changes: 11 additions & 11 deletions src/main/frontend/selection.js
@@ -1,7 +1,7 @@
// Copied from https://stackoverflow.com/a/20336116
function isDescendant(parent, child) {
const isDescendant=(parent, child)=> {
// from http://stackoverflow.com/questions/2234979/how-to-check-in-javascript-if-one-element-is-a-child-of-another
var node = child;
let node = child;
while (node != null) {
if (node == parent) {
return true;
Expand All @@ -11,10 +11,10 @@ function isDescendant(parent, child) {
return false;
}

function getNodesBetween(rootNode, node1, node2) {
var resultNodes = [];
var isBetweenNodes = false;
for (var i = 0; i < rootNode.childNodes.length; i+= 1) {
const getNodesBetween=(rootNode, node1, node2)=> {
const resultNodes = [];
let isBetweenNodes = false;
for (let i = 0; i < rootNode.childNodes.length; i+= 1) {
if (isDescendant(rootNode.childNodes[i], node1) || isDescendant(rootNode.childNodes[i], node2)) {
if (resultNodes.length == 0) {
isBetweenNodes = true;
Expand All @@ -39,9 +39,9 @@ function getNodesBetween(rootNode, node1, node2) {
}
}

export var getSelectedNodes = function (selectionAncestor, startNode) {
export const getSelectedNodes = (selectionAncestor, startNode)=> {
// from https://developer.mozilla.org/en-US/docs/Web/API/Selection
var selection = null;
let selection = null;
if(window.getSelection){
selection = window.getSelection();
} else if(document.selection){
Expand All @@ -52,13 +52,13 @@ export var getSelectedNodes = function (selectionAncestor, startNode) {
if (selection.isCollapsed) {
return [];
};
var node2 = selection.focusNode;
const node2 = selection.focusNode;
return getNodesBetween(selectionAncestor, startNode, node2);
}
};

export var clearSelection = function () {
var selection = null;
export const clearSelection = ()=> {
let selection = null;
if(window.getSelection){
selection = window.getSelection();
} else if(document.selection){
Expand Down

0 comments on commit 01eb908

Please sign in to comment.