refactor: fix lint on public/js/index.js

Signed-off-by: BoHong Li <raccoon@hackmd.io>
This commit is contained in:
BoHong Li 2019-08-02 01:03:16 +08:00
parent 8394ff58e9
commit 3505dcb1c8
No known key found for this signature in database
GPG Key ID: 06770355DC9ECD38

View File

@ -17,7 +17,7 @@ import List from 'list.js'
import Idle from '@hackmd/idle-js' import Idle from '@hackmd/idle-js'
import { Spinner } from 'spin.js'; import { Spinner } from 'spin.js'
import { import {
checkLoginStateChanged, checkLoginStateChanged,
@ -263,7 +263,7 @@ let visibleMD = false
let visibleLG = false let visibleLG = false
const isTouchDevice = 'ontouchstart' in document.documentElement const isTouchDevice = 'ontouchstart' in document.documentElement
let currentStatus = statusType.offline let currentStatus = statusType.offline
let lastInfo = { const lastInfo = {
needRestore: false, needRestore: false,
cursor: null, cursor: null,
scroll: null, scroll: null,
@ -289,14 +289,14 @@ let lastInfo = {
let personalInfo = {} let personalInfo = {}
let onlineUsers = [] let onlineUsers = []
const fileTypes = { const fileTypes = {
'pl': 'perl', pl: 'perl',
'cgi': 'perl', cgi: 'perl',
'js': 'javascript', js: 'javascript',
'php': 'php', php: 'php',
'sh': 'bash', sh: 'bash',
'rb': 'ruby', rb: 'ruby',
'html': 'html', html: 'html',
'py': 'python' py: 'python'
} }
// editor settings // editor settings
@ -956,8 +956,8 @@ ui.toolbar.export.dropbox.click(function () {
var options = { var options = {
files: [ files: [
{ {
'url': noteurl + '/download', url: noteurl + '/download',
'filename': filename filename: filename
} }
], ],
error: function (errorMessage) { error: function (errorMessage) {
@ -1788,29 +1788,29 @@ var authorMarks = {} // temp variable
var addTextMarkers = [] // temp variable var addTextMarkers = [] // temp variable
function updateInfo (data) { function updateInfo (data) {
// console.log(data); // console.log(data);
if (data.hasOwnProperty('createtime') && window.createtime !== data.createtime) { if (Object.hasOwnProperty.call(data, 'createtime') && window.createtime !== data.createtime) {
window.createtime = data.createtime window.createtime = data.createtime
updateLastChange() updateLastChange()
} }
if (data.hasOwnProperty('updatetime') && window.lastchangetime !== data.updatetime) { if (Object.hasOwnProperty.call(data, 'updatetime') && window.lastchangetime !== data.updatetime) {
window.lastchangetime = data.updatetime window.lastchangetime = data.updatetime
updateLastChange() updateLastChange()
} }
if (data.hasOwnProperty('owner') && window.owner !== data.owner) { if (Object.hasOwnProperty.call(data, 'owner') && window.owner !== data.owner) {
window.owner = data.owner window.owner = data.owner
window.ownerprofile = data.ownerprofile window.ownerprofile = data.ownerprofile
updateOwner() updateOwner()
} }
if (data.hasOwnProperty('lastchangeuser') && window.lastchangeuser !== data.lastchangeuser) { if (Object.hasOwnProperty.call(data, 'lastchangeuser') && window.lastchangeuser !== data.lastchangeuser) {
window.lastchangeuser = data.lastchangeuser window.lastchangeuser = data.lastchangeuser
window.lastchangeuserprofile = data.lastchangeuserprofile window.lastchangeuserprofile = data.lastchangeuserprofile
updateLastChangeUser() updateLastChangeUser()
updateOwner() updateOwner()
} }
if (data.hasOwnProperty('authors') && authors !== data.authors) { if (Object.hasOwnProperty.call(data, 'authors') && authors !== data.authors) {
authors = data.authors authors = data.authors
} }
if (data.hasOwnProperty('authorship') && authorship !== data.authorship) { if (Object.hasOwnProperty.call(data, 'authorship') && authorship !== data.authorship) {
authorship = data.authorship authorship = data.authorship
updateAuthorship() updateAuthorship()
} }
@ -1855,7 +1855,7 @@ function updateAuthorshipInner () {
authorMarks = {} authorMarks = {}
for (let i = 0; i < authorship.length; i++) { for (let i = 0; i < authorship.length; i++) {
var atom = authorship[i] var atom = authorship[i]
let author = authors[atom[0]] const author = authors[atom[0]]
if (author) { if (author) {
var prePos = editor.posFromIndex(atom[1]) var prePos = editor.posFromIndex(atom[1])
var preLine = editor.getLine(prePos.line) var preLine = editor.getLine(prePos.line)
@ -1873,7 +1873,7 @@ function updateAuthorshipInner () {
if (prePos.ch === preLine.length) { if (prePos.ch === preLine.length) {
startLine++ startLine++
} else if (prePos.ch !== 0) { } else if (prePos.ch !== 0) {
let mark = initMarkAndCheckGutter(authorMarks[prePos.line], author, atom[3]) const mark = initMarkAndCheckGutter(authorMarks[prePos.line], author, atom[3])
var _postPos = { var _postPos = {
line: prePos.line, line: prePos.line,
ch: preLine.length ch: preLine.length
@ -1890,7 +1890,7 @@ function updateAuthorshipInner () {
if (postPos.ch === 0) { if (postPos.ch === 0) {
endLine-- endLine--
} else if (postPos.ch !== postLine.length) { } else if (postPos.ch !== postLine.length) {
let mark = initMarkAndCheckGutter(authorMarks[postPos.line], author, atom[3]) const mark = initMarkAndCheckGutter(authorMarks[postPos.line], author, atom[3])
var _prePos = { var _prePos = {
line: postPos.line, line: postPos.line,
ch: 0 ch: 0
@ -1910,7 +1910,7 @@ function updateAuthorshipInner () {
} }
} }
} else { } else {
let mark = initMarkAndCheckGutter(authorMarks[prePos.line], author, atom[3]) const mark = initMarkAndCheckGutter(authorMarks[prePos.line], author, atom[3])
if (JSON.stringify(prePos) !== JSON.stringify(postPos)) { if (JSON.stringify(prePos) !== JSON.stringify(postPos)) {
mark.textmarkers.push({ mark.textmarkers.push({
userid: author.userid, userid: author.userid,
@ -1923,15 +1923,15 @@ function updateAuthorshipInner () {
} }
addTextMarkers = [] addTextMarkers = []
editor.eachLine(iterateLine) editor.eachLine(iterateLine)
var allTextMarks = editor.getAllMarks() const allTextMarks = editor.getAllMarks()
for (let i = 0; i < allTextMarks.length; i++) { for (let i = 0; i < allTextMarks.length; i++) {
let _textMarker = allTextMarks[i] const _textMarker = allTextMarks[i]
var pos = _textMarker.find() const pos = _textMarker.find()
var found = false let found = false
for (let j = 0; j < addTextMarkers.length; j++) { for (let j = 0; j < addTextMarkers.length; j++) {
let textMarker = addTextMarkers[j] const textMarker = addTextMarkers[j]
let author = authors[textMarker.userid] const author = authors[textMarker.userid]
let className = 'authorship-inline-' + author.color.substr(1) const className = 'authorship-inline-' + author.color.substr(1)
var obj = { var obj = {
from: textMarker.pos[0], from: textMarker.pos[0],
to: textMarker.pos[1] to: textMarker.pos[1]
@ -1949,12 +1949,12 @@ function updateAuthorshipInner () {
} }
} }
for (let i = 0; i < addTextMarkers.length; i++) { for (let i = 0; i < addTextMarkers.length; i++) {
let textMarker = addTextMarkers[i] const textMarker = addTextMarkers[i]
let author = authors[textMarker.userid] const author = authors[textMarker.userid]
const rgbcolor = hex2rgb(author.color) const rgbcolor = hex2rgb(author.color)
const colorString = `rgba(${rgbcolor.red},${rgbcolor.green},${rgbcolor.blue},0.7)` const colorString = `rgba(${rgbcolor.red},${rgbcolor.green},${rgbcolor.blue},0.7)`
const styleString = `background-image: linear-gradient(to top, ${colorString} 1px, transparent 1px);` const styleString = `background-image: linear-gradient(to top, ${colorString} 1px, transparent 1px);`
let className = `authorship-inline-${author.color.substr(1)}` const className = `authorship-inline-${author.color.substr(1)}`
const rule = `.${className} { ${styleString} }` const rule = `.${className} { ${styleString} }`
addStyleRule(rule) addStyleRule(rule)
editor.markText(textMarker.pos[0], textMarker.pos[1], { editor.markText(textMarker.pos[0], textMarker.pos[1], {
@ -1964,11 +1964,11 @@ function updateAuthorshipInner () {
} }
} }
function iterateLine (line) { function iterateLine (line) {
var lineNumber = line.lineNo() const lineNumber = line.lineNo()
var currMark = authorMarks[lineNumber] const currMark = authorMarks[lineNumber]
var author = currMark ? authors[currMark.gutter.userid] : null const author = currMark ? authors[currMark.gutter.userid] : null
if (currMark && author) { if (currMark && author) {
let className = 'authorship-gutter-' + author.color.substr(1) const className = 'authorship-gutter-' + author.color.substr(1)
const gutters = line.gutterMarkers const gutters = line.gutterMarkers
if (!gutters || !gutters['authorship-gutters'] || if (!gutters || !gutters['authorship-gutters'] ||
!gutters['authorship-gutters'].className || !gutters['authorship-gutters'].className ||
@ -1976,7 +1976,7 @@ function iterateLine (line) {
const styleString = `border-left: 3px solid ${author.color}; height: ${defaultTextHeight}px; margin-left: 3px;` const styleString = `border-left: 3px solid ${author.color}; height: ${defaultTextHeight}px; margin-left: 3px;`
const rule = `.${className} { ${styleString} }` const rule = `.${className} { ${styleString} }`
addStyleRule(rule) addStyleRule(rule)
var gutter = $('<div>', { const gutter = $('<div>', {
class: 'authorship-gutter ' + className, class: 'authorship-gutter ' + className,
title: author.name title: author.name
}) })
@ -1986,8 +1986,8 @@ function iterateLine (line) {
editor.setGutterMarker(line, 'authorship-gutters', null) editor.setGutterMarker(line, 'authorship-gutters', null)
} }
if (currMark && currMark.textmarkers.length > 0) { if (currMark && currMark.textmarkers.length > 0) {
for (var i = 0; i < currMark.textmarkers.length; i++) { for (let i = 0; i < currMark.textmarkers.length; i++) {
let textMarker = currMark.textmarkers[i] const textMarker = currMark.textmarkers[i]
if (textMarker.userid !== currMark.gutter.userid) { if (textMarker.userid !== currMark.gutter.userid) {
addTextMarkers.push(textMarker) addTextMarkers.push(textMarker)
} }
@ -1998,12 +1998,12 @@ editorInstance.on('update', function () {
$('.authorship-gutter:not([data-original-title])').tooltip({ $('.authorship-gutter:not([data-original-title])').tooltip({
container: '.CodeMirror-lines', container: '.CodeMirror-lines',
placement: 'right', placement: 'right',
delay: { 'show': 500, 'hide': 100 } delay: { show: 500, hide: 100 }
}) })
$('.authorship-inline:not([data-original-title])').tooltip({ $('.authorship-inline:not([data-original-title])').tooltip({
container: '.CodeMirror-lines', container: '.CodeMirror-lines',
placement: 'bottom', placement: 'bottom',
delay: { 'show': 500, 'hide': 100 } delay: { show: 500, hide: 100 }
}) })
// clear tooltip which described element has been removed // clear tooltip which described element has been removed
$('[id^="tooltip"]').each(function (index, element) { $('[id^="tooltip"]').each(function (index, element) {
@ -2064,7 +2064,7 @@ var cmClient = null
var synchronized_ = null var synchronized_ = null
function havePendingOperation () { function havePendingOperation () {
return !!((cmClient && cmClient.state && cmClient.state.hasOwnProperty('outstanding'))) return !!((cmClient && cmClient.state && Object.hasOwnProperty.call(cmClient.state, 'outstanding')))
} }
socket.on('doc', function (obj) { socket.on('doc', function (obj) {
@ -2224,7 +2224,7 @@ function updateOnlineStatus () {
break break
} }
} }
let id = items[i].values().id const id = items[i].values().id
if (found) { if (found) {
onlineUserList.get('id', id)[0].values(_onlineUsers[foundindex]) onlineUserList.get('id', id)[0].values(_onlineUsers[foundindex])
shortOnlineUserList.get('id', id)[0].values(_onlineUsers[foundindex]) shortOnlineUserList.get('id', id)[0].values(_onlineUsers[foundindex])
@ -2418,19 +2418,19 @@ function buildCursor (user) {
break break
} }
if ($('div[data-clientid="' + user.id + '"]').length <= 0) { if ($('div[data-clientid="' + user.id + '"]').length <= 0) {
let cursor = $('<div data-clientid="' + user.id + '" class="CodeMirror-other-cursor" style="display:none;"></div>') const cursor = $('<div data-clientid="' + user.id + '" class="CodeMirror-other-cursor" style="display:none;"></div>')
cursor.attr('data-line', user.cursor.line) cursor.attr('data-line', user.cursor.line)
cursor.attr('data-ch', user.cursor.ch) cursor.attr('data-ch', user.cursor.ch)
cursor.attr('data-offset-left', 0) cursor.attr('data-offset-left', 0)
cursor.attr('data-offset-top', 0) cursor.attr('data-offset-top', 0)
let cursorbar = $('<div class="cursorbar">&nbsp;</div>') const cursorbar = $('<div class="cursorbar">&nbsp;</div>')
cursorbar[0].style.height = defaultTextHeight + 'px' cursorbar[0].style.height = defaultTextHeight + 'px'
cursorbar[0].style.borderLeft = '2px solid ' + user.color cursorbar[0].style.borderLeft = '2px solid ' + user.color
var icon = '<i class="fa ' + iconClass + '"></i>' var icon = '<i class="fa ' + iconClass + '"></i>'
let cursortag = $('<div class="cursortag">' + icon + '&nbsp;<span class="name">' + user.name + '</span></div>') const cursortag = $('<div class="cursortag">' + icon + '&nbsp;<span class="name">' + user.name + '</span></div>')
// cursortag[0].style.background = color; // cursortag[0].style.background = color;
cursortag[0].style.color = user.color cursortag[0].style.color = user.color
@ -2486,15 +2486,15 @@ function buildCursor (user) {
checkCursorTag(coord, cursortag) checkCursorTag(coord, cursortag)
} else { } else {
let cursor = $('div[data-clientid="' + user.id + '"]') const cursor = $('div[data-clientid="' + user.id + '"]')
cursor.attr('data-line', user.cursor.line) cursor.attr('data-line', user.cursor.line)
cursor.attr('data-ch', user.cursor.ch) cursor.attr('data-ch', user.cursor.ch)
let cursorbar = cursor.find('.cursorbar') const cursorbar = cursor.find('.cursorbar')
cursorbar[0].style.height = defaultTextHeight + 'px' cursorbar[0].style.height = defaultTextHeight + 'px'
cursorbar[0].style.borderLeft = '2px solid ' + user.color cursorbar[0].style.borderLeft = '2px solid ' + user.color
let cursortag = cursor.find('.cursortag') const cursortag = cursor.find('.cursortag')
cursortag.find('i').removeClass().addClass('fa').addClass(iconClass) cursortag.find('i').removeClass().addClass('fa').addClass(iconClass)
cursortag.find('.name').text(user.name) cursortag.find('.name').text(user.name)
@ -2503,8 +2503,8 @@ function buildCursor (user) {
cursor[0].style.top = coord.top + 'px' cursor[0].style.top = coord.top + 'px'
} else { } else {
cursor.animate({ cursor.animate({
'left': coord.left, left: coord.left,
'top': coord.top top: coord.top
}, { }, {
duration: cursorAnimatePeriod, duration: cursorAnimatePeriod,
queue: false queue: false
@ -2713,8 +2713,8 @@ function restoreInfo () {
$(window).scrollLeft(lastInfo.edit.scroll.left) $(window).scrollLeft(lastInfo.edit.scroll.left)
$(window).scrollTop(lastInfo.edit.scroll.top) $(window).scrollTop(lastInfo.edit.scroll.top)
} else { } else {
let left = lastInfo.edit.scroll.left const left = lastInfo.edit.scroll.left
let top = lastInfo.edit.scroll.top const top = lastInfo.edit.scroll.top
editor.scrollIntoView() editor.scrollIntoView()
editor.scrollTo(left, top) editor.scrollTo(left, top)
} }
@ -2724,8 +2724,8 @@ function restoreInfo () {
$(window).scrollTop(lastInfo.view.scroll.top) $(window).scrollTop(lastInfo.view.scroll.top)
break break
case modeType.both: case modeType.both:
let left = lastInfo.edit.scroll.left const left = lastInfo.edit.scroll.left
let top = lastInfo.edit.scroll.top const top = lastInfo.edit.scroll.top
editor.scrollIntoView() editor.scrollIntoView()
editor.scrollTo(left, top) editor.scrollTo(left, top)
ui.area.view.scrollLeft(lastInfo.view.scroll.left) ui.area.view.scrollLeft(lastInfo.view.scroll.left)
@ -2847,8 +2847,8 @@ function partialUpdate (src, tar, des) {
for (let i = 0; i < tar.length; i++) { for (let i = 0; i < tar.length; i++) {
// copyAttribute(src[i], des[i], 'data-startline'); // copyAttribute(src[i], des[i], 'data-startline');
// copyAttribute(src[i], des[i], 'data-endline'); // copyAttribute(src[i], des[i], 'data-endline');
let rawSrc = cloneAndRemoveDataAttr(src[i]) const rawSrc = cloneAndRemoveDataAttr(src[i])
let rawTar = cloneAndRemoveDataAttr(tar[i]) const rawTar = cloneAndRemoveDataAttr(tar[i])
if (!rawSrc || !rawTar || rawSrc.outerHTML !== rawTar.outerHTML) { if (!rawSrc || !rawTar || rawSrc.outerHTML !== rawTar.outerHTML) {
start = i start = i
break break
@ -2860,8 +2860,8 @@ function partialUpdate (src, tar, des) {
for (let i = 0; i < src.length; i++) { for (let i = 0; i < src.length; i++) {
// copyAttribute(src[i], des[i], 'data-startline'); // copyAttribute(src[i], des[i], 'data-startline');
// copyAttribute(src[i], des[i], 'data-endline'); // copyAttribute(src[i], des[i], 'data-endline');
let rawSrc = cloneAndRemoveDataAttr(src[i]) const rawSrc = cloneAndRemoveDataAttr(src[i])
let rawTar = cloneAndRemoveDataAttr(tar[i]) const rawTar = cloneAndRemoveDataAttr(tar[i])
if (!rawSrc || !rawTar || rawSrc.outerHTML !== rawTar.outerHTML) { if (!rawSrc || !rawTar || rawSrc.outerHTML !== rawTar.outerHTML) {
start = i start = i
break break
@ -2869,12 +2869,12 @@ function partialUpdate (src, tar, des) {
} }
// tar end // tar end
for (let i = 1; i <= tar.length + 1; i++) { for (let i = 1; i <= tar.length + 1; i++) {
let srcLength = src.length const srcLength = src.length
let tarLength = tar.length const tarLength = tar.length
// copyAttribute(src[srcLength - i], des[srcLength - i], 'data-startline'); // copyAttribute(src[srcLength - i], des[srcLength - i], 'data-startline');
// copyAttribute(src[srcLength - i], des[srcLength - i], 'data-endline'); // copyAttribute(src[srcLength - i], des[srcLength - i], 'data-endline');
let rawSrc = cloneAndRemoveDataAttr(src[srcLength - i]) const rawSrc = cloneAndRemoveDataAttr(src[srcLength - i])
let rawTar = cloneAndRemoveDataAttr(tar[tarLength - i]) const rawTar = cloneAndRemoveDataAttr(tar[tarLength - i])
if (!rawSrc || !rawTar || rawSrc.outerHTML !== rawTar.outerHTML) { if (!rawSrc || !rawTar || rawSrc.outerHTML !== rawTar.outerHTML) {
tarEnd = tar.length - i tarEnd = tar.length - i
break break
@ -2882,12 +2882,12 @@ function partialUpdate (src, tar, des) {
} }
// src end // src end
for (let i = 1; i <= src.length + 1; i++) { for (let i = 1; i <= src.length + 1; i++) {
let srcLength = src.length const srcLength = src.length
let tarLength = tar.length const tarLength = tar.length
// copyAttribute(src[srcLength - i], des[srcLength - i], 'data-startline'); // copyAttribute(src[srcLength - i], des[srcLength - i], 'data-startline');
// copyAttribute(src[srcLength - i], des[srcLength - i], 'data-endline'); // copyAttribute(src[srcLength - i], des[srcLength - i], 'data-endline');
let rawSrc = cloneAndRemoveDataAttr(src[srcLength - i]) const rawSrc = cloneAndRemoveDataAttr(src[srcLength - i])
let rawTar = cloneAndRemoveDataAttr(tar[tarLength - i]) const rawTar = cloneAndRemoveDataAttr(tar[tarLength - i])
if (!rawSrc || !rawTar || rawSrc.outerHTML !== rawTar.outerHTML) { if (!rawSrc || !rawTar || rawSrc.outerHTML !== rawTar.outerHTML) {
srcEnd = src.length - i srcEnd = src.length - i
break break
@ -3318,22 +3318,22 @@ $(editor.getInputField())
'textComplete:show': function (e) { 'textComplete:show': function (e) {
$(this).data('autocompleting', true) $(this).data('autocompleting', true)
editor.setOption('extraKeys', { editor.setOption('extraKeys', {
'Up': function () { Up: function () {
return false return false
}, },
'Right': function () { Right: function () {
editor.doc.cm.execCommand('goCharRight') editor.doc.cm.execCommand('goCharRight')
}, },
'Down': function () { Down: function () {
return false return false
}, },
'Left': function () { Left: function () {
editor.doc.cm.execCommand('goCharLeft') editor.doc.cm.execCommand('goCharLeft')
}, },
'Enter': function () { Enter: function () {
return false return false
}, },
'Backspace': function () { Backspace: function () {
editor.doc.cm.execCommand('delCharBefore') editor.doc.cm.execCommand('delCharBefore')
} }
}) })