lint: linestylefilter and rjquery.js

pull/4653/head
John McLear 2020-12-21 23:58:58 +00:00
parent c38c34bef4
commit 46dc943101
2 changed files with 77 additions and 76 deletions

View File

@ -1,3 +1,5 @@
'use strict';
/** /**
* This code is mostly from the old Etherpad. Please help us to comment this code. * This code is mostly from the old Etherpad. Please help us to comment this code.
* This helps other people to understand this code better and helps them to improve it. * This helps other people to understand this code better and helps them to improve it.
@ -31,7 +33,6 @@
const Changeset = require('./Changeset'); const Changeset = require('./Changeset');
const hooks = require('./pluginfw/hooks'); const hooks = require('./pluginfw/hooks');
const linestylefilter = {}; const linestylefilter = {};
const _ = require('./underscore');
const AttributeManager = require('./AttributeManager'); const AttributeManager = require('./AttributeManager');
const padutils = require('./pad_utils').padutils; const padutils = require('./pad_utils').padutils;
@ -45,32 +46,30 @@ linestylefilter.ATTRIB_CLASSES = {
const lineAttributeMarker = 'lineAttribMarker'; const lineAttributeMarker = 'lineAttribMarker';
exports.lineAttributeMarker = lineAttributeMarker; exports.lineAttributeMarker = lineAttributeMarker;
linestylefilter.getAuthorClassName = function (author) { linestylefilter.getAuthorClassName = (author) => `author-${author.replace(/[^a-y0-9]/g, (c) => {
return `author-${author.replace(/[^a-y0-9]/g, (c) => { if (c === '.') return '-';
if (c == '.') return '-';
return `z${c.charCodeAt(0)}z`; return `z${c.charCodeAt(0)}z`;
})}`; })}`;
};
// lineLength is without newline; aline includes newline, // lineLength is without newline; aline includes newline,
// but may be falsy if lineLength == 0 // but may be falsy if lineLength == 0
linestylefilter.getLineStyleFilter = function (lineLength, aline, textAndClassFunc, apool) { linestylefilter.getLineStyleFilter = (lineLength, aline, textAndClassFunc, apool) => {
// Plugin Hook to add more Attrib Classes // Plugin Hook to add more Attrib Classes
for (const attribClasses of hooks.callAll('aceAttribClasses', linestylefilter.ATTRIB_CLASSES)) { for (const attribClasses of hooks.callAll('aceAttribClasses', linestylefilter.ATTRIB_CLASSES)) {
Object.assign(linestylefilter.ATTRIB_CLASSES, attribClasses); Object.assign(linestylefilter.ATTRIB_CLASSES, attribClasses);
} }
if (lineLength == 0) return textAndClassFunc; if (lineLength === 0) return textAndClassFunc;
const nextAfterAuthorColors = textAndClassFunc; const nextAfterAuthorColors = textAndClassFunc;
const authorColorFunc = (function () { const authorColorFunc = (() => {
const lineEnd = lineLength; const lineEnd = lineLength;
let curIndex = 0; let curIndex = 0;
let extraClasses; let extraClasses;
let leftInAuthor; let leftInAuthor;
function attribsToClasses(attribs) { const attribsToClasses = (attribs) => {
let classes = ''; let classes = '';
let isLineAttribMarker = false; let isLineAttribMarker = false;
@ -81,14 +80,14 @@ linestylefilter.getLineStyleFilter = function (lineLength, aline, textAndClassFu
if (key) { if (key) {
const value = apool.getAttribValue(n); const value = apool.getAttribValue(n);
if (value) { if (value) {
if (!isLineAttribMarker && _.indexOf(AttributeManager.lineAttributes, key) >= 0) { if (!isLineAttribMarker && AttributeManager.lineAttributes.indexOf(key) >= 0) {
isLineAttribMarker = true; isLineAttribMarker = true;
} }
if (key == 'author') { if (key === 'author') {
classes += ` ${linestylefilter.getAuthorClassName(value)}`; classes += ` ${linestylefilter.getAuthorClassName(value)}`;
} else if (key == 'list') { } else if (key === 'list') {
classes += ` list:${value}`; classes += ` list:${value}`;
} else if (key == 'start') { } else if (key === 'start') {
// Needed to introduce the correct Ordered list item start number on import // Needed to introduce the correct Ordered list item start number on import
classes += ` start:${value}`; classes += ` start:${value}`;
} else if (linestylefilter.ATTRIB_CLASSES[key]) { } else if (linestylefilter.ATTRIB_CLASSES[key]) {
@ -106,37 +105,38 @@ linestylefilter.getLineStyleFilter = function (lineLength, aline, textAndClassFu
if (isLineAttribMarker) classes += ` ${lineAttributeMarker}`; if (isLineAttribMarker) classes += ` ${lineAttributeMarker}`;
return classes.substring(1); return classes.substring(1);
} };
const attributionIter = Changeset.opIterator(aline); const attributionIter = Changeset.opIterator(aline);
let nextOp, nextOpClasses; let nextOp, nextOpClasses;
function goNextOp() { const goNextOp = () => {
nextOp = attributionIter.next(); nextOp = attributionIter.next();
nextOpClasses = (nextOp.opcode && attribsToClasses(nextOp.attribs)); nextOpClasses = (nextOp.opcode && attribsToClasses(nextOp.attribs));
} };
goNextOp(); goNextOp();
function nextClasses() { const nextClasses = () => {
if (curIndex < lineEnd) { if (curIndex < lineEnd) {
extraClasses = nextOpClasses; extraClasses = nextOpClasses;
leftInAuthor = nextOp.chars; leftInAuthor = nextOp.chars;
goNextOp(); goNextOp();
while (nextOp.opcode && nextOpClasses == extraClasses) { while (nextOp.opcode && nextOpClasses === extraClasses) {
leftInAuthor += nextOp.chars; leftInAuthor += nextOp.chars;
goNextOp(); goNextOp();
} }
} }
} };
nextClasses(); nextClasses();
return function (txt, cls) { return (txt, cls) => {
const disableAuthColorForThisLine = hooks.callAll('disableAuthorColorsForThisLine', { const disableAuthColorForThisLine = hooks.callAll('disableAuthorColorsForThisLine', {
linestylefilter, linestylefilter,
text: txt, text: txt,
class: cls, class: cls,
}, ' ', ' ', ''); }, ' ', ' ', '');
const disableAuthors = (disableAuthColorForThisLine == null || disableAuthColorForThisLine.length == 0) ? false : disableAuthColorForThisLine[0]; const disableAuthors = (disableAuthColorForThisLine == null ||
disableAuthColorForThisLine.length === 0) ? false : disableAuthColorForThisLine[0];
while (txt.length > 0) { while (txt.length > 0) {
if (leftInAuthor <= 0 || disableAuthors) { if (leftInAuthor <= 0 || disableAuthors) {
// prevent infinite loop if something funny's going on // prevent infinite loop if something funny's going on
@ -151,7 +151,7 @@ linestylefilter.getLineStyleFilter = function (lineLength, aline, textAndClassFu
nextAfterAuthorColors(curTxt, (cls && `${cls} `) + extraClasses); nextAfterAuthorColors(curTxt, (cls && `${cls} `) + extraClasses);
curIndex += spanSize; curIndex += spanSize;
leftInAuthor -= spanSize; leftInAuthor -= spanSize;
if (leftInAuthor == 0) { if (leftInAuthor === 0) {
nextClasses(); nextClasses();
} }
} }
@ -160,7 +160,7 @@ linestylefilter.getLineStyleFilter = function (lineLength, aline, textAndClassFu
return authorColorFunc; return authorColorFunc;
}; };
linestylefilter.getAtSignSplitterFilter = function (lineText, textAndClassFunc) { linestylefilter.getAtSignSplitterFilter = (lineText, textAndClassFunc) => {
const at = /@/g; const at = /@/g;
at.lastIndex = 0; at.lastIndex = 0;
let splitPoints = null; let splitPoints = null;
@ -177,8 +177,7 @@ linestylefilter.getAtSignSplitterFilter = function (lineText, textAndClassFunc)
return linestylefilter.textAndClassFuncSplitter(textAndClassFunc, splitPoints); return linestylefilter.textAndClassFuncSplitter(textAndClassFunc, splitPoints);
}; };
linestylefilter.getRegexpFilter = function (regExp, tag) { linestylefilter.getRegexpFilter = (regExp, tag) => (lineText, textAndClassFunc) => {
return function (lineText, textAndClassFunc) {
regExp.lastIndex = 0; regExp.lastIndex = 0;
let regExpMatchs = null; let regExpMatchs = null;
let splitPoints = null; let splitPoints = null;
@ -196,7 +195,7 @@ linestylefilter.getRegexpFilter = function (regExp, tag) {
if (!regExpMatchs) return textAndClassFunc; if (!regExpMatchs) return textAndClassFunc;
function regExpMatchForIndex(idx) { const regExpMatchForIndex = (idx) => {
for (let k = 0; k < regExpMatchs.length; k++) { for (let k = 0; k < regExpMatchs.length; k++) {
const u = regExpMatchs[k]; const u = regExpMatchs[k];
if (idx >= u[0] && idx < u[0] + u[1].length) { if (idx >= u[0] && idx < u[0] + u[1].length) {
@ -204,11 +203,11 @@ linestylefilter.getRegexpFilter = function (regExp, tag) {
} }
} }
return false; return false;
} };
const handleRegExpMatchsAfterSplit = (function () { const handleRegExpMatchsAfterSplit = (() => {
let curIndex = 0; let curIndex = 0;
return function (txt, cls) { return (txt, cls) => {
const txtlen = txt.length; const txtlen = txt.length;
let newCls = cls; let newCls = cls;
const regExpMatch = regExpMatchForIndex(curIndex); const regExpMatch = regExpMatchForIndex(curIndex);
@ -221,22 +220,23 @@ linestylefilter.getRegexpFilter = function (regExp, tag) {
})(); })();
return linestylefilter.textAndClassFuncSplitter(handleRegExpMatchsAfterSplit, splitPoints); return linestylefilter.textAndClassFuncSplitter(handleRegExpMatchsAfterSplit, splitPoints);
};
}; };
linestylefilter.getURLFilter = linestylefilter.getRegexpFilter(padutils.urlRegex, 'url'); linestylefilter.getURLFilter = linestylefilter.getRegexpFilter(padutils.urlRegex, 'url');
linestylefilter.textAndClassFuncSplitter = function (func, splitPointsOpt) { linestylefilter.textAndClassFuncSplitter = (func, splitPointsOpt) => {
let nextPointIndex = 0; let nextPointIndex = 0;
let idx = 0; let idx = 0;
// don't split at 0 // don't split at 0
while (splitPointsOpt && nextPointIndex < splitPointsOpt.length && splitPointsOpt[nextPointIndex] == 0) { while (splitPointsOpt &&
nextPointIndex < splitPointsOpt.length &&
splitPointsOpt[nextPointIndex] === 0) {
nextPointIndex++; nextPointIndex++;
} }
function spanHandler(txt, cls) { const spanHandler = (txt, cls) => {
if ((!splitPointsOpt) || nextPointIndex >= splitPointsOpt.length) { if ((!splitPointsOpt) || nextPointIndex >= splitPointsOpt.length) {
func(txt, cls); func(txt, cls);
idx += txt.length; idx += txt.length;
@ -247,7 +247,7 @@ linestylefilter.textAndClassFuncSplitter = function (func, splitPointsOpt) {
if (pointLocInSpan >= txtlen) { if (pointLocInSpan >= txtlen) {
func(txt, cls); func(txt, cls);
idx += txt.length; idx += txt.length;
if (pointLocInSpan == txtlen) { if (pointLocInSpan === txtlen) {
nextPointIndex++; nextPointIndex++;
} }
} else { } else {
@ -260,18 +260,18 @@ linestylefilter.textAndClassFuncSplitter = function (func, splitPointsOpt) {
spanHandler(txt.substring(pointLocInSpan), cls); spanHandler(txt.substring(pointLocInSpan), cls);
} }
} }
} };
return spanHandler; return spanHandler;
}; };
linestylefilter.getFilterStack = function (lineText, textAndClassFunc, abrowser) { linestylefilter.getFilterStack = (lineText, textAndClassFunc, abrowser) => {
let func = linestylefilter.getURLFilter(lineText, textAndClassFunc); let func = linestylefilter.getURLFilter(lineText, textAndClassFunc);
const hookFilters = hooks.callAll('aceGetFilterStack', { const hookFilters = hooks.callAll('aceGetFilterStack', {
linestylefilter, linestylefilter,
browser: abrowser, browser: abrowser,
}); });
_.map(hookFilters, (hookFilter) => { hookFilters.map((hookFilter) => {
func = hookFilter(lineText, func); func = hookFilter(lineText, func);
}); });
@ -279,16 +279,16 @@ linestylefilter.getFilterStack = function (lineText, textAndClassFunc, abrowser)
}; };
// domLineObj is like that returned by domline.createDomLine // domLineObj is like that returned by domline.createDomLine
linestylefilter.populateDomLine = function (textLine, aline, apool, domLineObj) { linestylefilter.populateDomLine = (textLine, aline, apool, domLineObj) => {
// remove final newline from text if any // remove final newline from text if any
let text = textLine; let text = textLine;
if (text.slice(-1) == '\n') { if (text.slice(-1) === '\n') {
text = text.substring(0, text.length - 1); text = text.substring(0, text.length - 1);
} }
function textAndClassFunc(tokenText, tokenClass) { const textAndClassFunc = (tokenText, tokenClass) => {
domLineObj.appendSpan(tokenText, tokenClass); domLineObj.appendSpan(tokenText, tokenClass);
} };
let func = linestylefilter.getFilterStack(text, textAndClassFunc); let func = linestylefilter.getFilterStack(text, textAndClassFunc);
func = linestylefilter.getLineStyleFilter(text.length, aline, func, apool); func = linestylefilter.getLineStyleFilter(text.length, aline, func, apool);

View File

@ -1,4 +1,5 @@
// Proviedes a require'able version of jQuery without leaking $ and jQuery; 'use strict';
// Provides a require'able version of jQuery without leaking $ and jQuery;
window.$ = require('./jquery'); window.$ = require('./jquery');
const jq = window.$.noConflict(true); const jq = window.$.noConflict(true);
exports.jQuery = exports.$ = jq; exports.jQuery = exports.$ = jq;