diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index bbc755ad3..a5e4f96ae 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -291,6 +291,9 @@ importers: '@types/jquery': specifier: ^3.5.30 version: 3.5.30 + '@types/js-cookie': + specifier: ^3.0.6 + version: 3.0.6 '@types/jsdom': specifier: ^21.1.7 version: 21.1.7 @@ -1501,6 +1504,9 @@ packages: '@types/jquery@3.5.30': resolution: {integrity: sha512-nbWKkkyb919DOUxjmRVk8vwtDb0/k8FKncmUKFi+NY+QXqWltooxTrswvz4LspQwxvLdvzBN1TImr6cw3aQx2A==} + '@types/js-cookie@3.0.6': + resolution: {integrity: sha512-wkw9yd1kEXOPnvEeEV1Go1MmxtBJL0RR79aOTAApecWFVu7w0NNXNqhcWgvw2YgZDYadliXkl14pa3WXw5jlCQ==} + '@types/jsdom@21.1.7': resolution: {integrity: sha512-yOriVnggzrnQ3a9OKOCxaVuSug3w3/SbOj5i7VwXWZEyUNl3bLF9V3MfxGbZKuwqJOQyRfqXyROBB1CoZLFWzA==} @@ -5685,6 +5691,8 @@ snapshots: dependencies: '@types/sizzle': 2.3.8 + '@types/js-cookie@3.0.6': {} + '@types/jsdom@21.1.7': dependencies: '@types/node': 22.4.0 diff --git a/src/node/db/API.ts b/src/node/db/API.ts index 9ce84fc51..e9f445560 100644 --- a/src/node/db/API.ts +++ b/src/node/db/API.ts @@ -20,7 +20,7 @@ */ const Changeset = require('../../static/js/Changeset'); -const ChatMessage = require('../../static/js/ChatMessage'); +import ChatMessage from '../../static/js/ChatMessage'; const CustomError = require('../utils/customError'); const padManager = require('./PadManager'); const padMessageHandler = require('../handler/PadMessageHandler'); diff --git a/src/node/db/AuthorManager.ts b/src/node/db/AuthorManager.ts index 2f4e7d751..4bcfa2c0d 100644 --- a/src/node/db/AuthorManager.ts +++ b/src/node/db/AuthorManager.ts @@ -21,8 +21,8 @@ const db = require('./DB'); const CustomError = require('../utils/customError'); -const hooks = require('../../static/js/pluginfw/hooks.js'); -const {randomString, padutils: {warnDeprecated}} = require('../../static/js/pad_utils'); +const hooks = require('../../static/js/pluginfw/hooks'); +import padutils, {randomString} from "../../static/js/pad_utils"; exports.getColorPalette = () => [ '#ffc7c7', @@ -169,7 +169,7 @@ exports.getAuthorId = async (token: string, user: object) => { * @param {String} token The token */ exports.getAuthor4Token = async (token: string) => { - warnDeprecated( + padutils.warnDeprecated( 'AuthorManager.getAuthor4Token() is deprecated; use AuthorManager.getAuthorId() instead'); return await getAuthor4Token(token); }; diff --git a/src/node/db/GroupManager.ts b/src/node/db/GroupManager.ts index 0524c4eda..b8cb6db02 100644 --- a/src/node/db/GroupManager.ts +++ b/src/node/db/GroupManager.ts @@ -20,7 +20,7 @@ */ const CustomError = require('../utils/customError'); -const randomString = require('../../static/js/pad_utils').randomString; +import {randomString} from "../../static/js/pad_utils"; const db = require('./DB'); const padManager = require('./PadManager'); const sessionManager = require('./SessionManager'); diff --git a/src/node/db/Pad.ts b/src/node/db/Pad.ts index fa4af994d..e117bb343 100644 --- a/src/node/db/Pad.ts +++ b/src/node/db/Pad.ts @@ -7,10 +7,10 @@ import {MapArrayType} from "../types/MapType"; * The pad object, defined with joose */ -const AttributeMap = require('../../static/js/AttributeMap'); +import AttributeMap from '../../static/js/AttributeMap'; const Changeset = require('../../static/js/Changeset'); -const ChatMessage = require('../../static/js/ChatMessage'); -const AttributePool = require('../../static/js/AttributePool'); +import ChatMessage from '../../static/js/ChatMessage'; +import AttributePool from '../../static/js/AttributePool'; const Stream = require('../utils/Stream'); const assert = require('assert').strict; const db = require('./DB'); @@ -23,7 +23,7 @@ const CustomError = require('../utils/customError'); const readOnlyManager = require('./ReadOnlyManager'); const randomString = require('../utils/randomstring'); const hooks = require('../../static/js/pluginfw/hooks'); -const {padutils: {warnDeprecated}} = require('../../static/js/pad_utils'); +import pad_utils from "../../static/js/pad_utils"; const promises = require('../utils/promises'); /** @@ -40,7 +40,7 @@ exports.cleanText = (txt:string): string => txt.replace(/\r\n/g, '\n') class Pad { private db: Database; private atext: AText; - private pool: APool; + private pool: AttributePool; private head: number; private chatHead: number; private publicStatus: boolean; @@ -126,11 +126,11 @@ class Pad { pad: this, authorId, get author() { - warnDeprecated(`${hook} hook author context is deprecated; use authorId instead`); + pad_utils.warnDeprecated(`${hook} hook author context is deprecated; use authorId instead`); return this.authorId; }, set author(authorId) { - warnDeprecated(`${hook} hook author context is deprecated; use authorId instead`); + pad_utils.warnDeprecated(`${hook} hook author context is deprecated; use authorId instead`); this.authorId = authorId; }, ...this.head === 0 ? {} : { @@ -330,7 +330,7 @@ class Pad { * @param {?number} [time] - Message timestamp (milliseconds since epoch). Deprecated; use * `msgOrText.time` instead. */ - async appendChatMessage(msgOrText: string|typeof ChatMessage, authorId = null, time = null) { + async appendChatMessage(msgOrText: string| ChatMessage, authorId = null, time = null) { const msg = msgOrText instanceof ChatMessage ? msgOrText : new ChatMessage(msgOrText, authorId, time); this.chatHead++; @@ -437,11 +437,11 @@ class Pad { // let the plugins know the pad was copied await hooks.aCallAll('padCopy', { get originalPad() { - warnDeprecated('padCopy originalPad context property is deprecated; use srcPad instead'); + pad_utils.warnDeprecated('padCopy originalPad context property is deprecated; use srcPad instead'); return this.srcPad; }, get destinationID() { - warnDeprecated( + pad_utils.warnDeprecated( 'padCopy destinationID context property is deprecated; use dstPad.id instead'); return this.dstPad.id; }, @@ -538,11 +538,11 @@ class Pad { await hooks.aCallAll('padCopy', { get originalPad() { - warnDeprecated('padCopy originalPad context property is deprecated; use srcPad instead'); + pad_utils.warnDeprecated('padCopy originalPad context property is deprecated; use srcPad instead'); return this.srcPad; }, get destinationID() { - warnDeprecated( + pad_utils.warnDeprecated( 'padCopy destinationID context property is deprecated; use dstPad.id instead'); return this.dstPad.id; }, @@ -603,7 +603,7 @@ class Pad { p.push(padManager.removePad(padID)); p.push(hooks.aCallAll('padRemove', { get padID() { - warnDeprecated('padRemove padID context property is deprecated; use pad.id instead'); + pad_utils.warnDeprecated('padRemove padID context property is deprecated; use pad.id instead'); return this.pad.id; }, pad: this, diff --git a/src/node/db/SecurityManager.ts b/src/node/db/SecurityManager.ts index 326bf3659..c2f209a01 100644 --- a/src/node/db/SecurityManager.ts +++ b/src/node/db/SecurityManager.ts @@ -22,7 +22,7 @@ import {UserSettingsObject} from "../types/UserSettingsObject"; const authorManager = require('./AuthorManager'); -const hooks = require('../../static/js/pluginfw/hooks.js'); +const hooks = require('../../static/js/pluginfw/hooks'); const padManager = require('./PadManager'); const readOnlyManager = require('./ReadOnlyManager'); const sessionManager = require('./SessionManager'); @@ -30,7 +30,7 @@ const settings = require('../utils/Settings'); const webaccess = require('../hooks/express/webaccess'); const log4js = require('log4js'); const authLogger = log4js.getLogger('auth'); -const {padutils} = require('../../static/js/pad_utils'); +import padutils from '../../static/js/pad_utils' const DENY = Object.freeze({accessStatus: 'deny'}); diff --git a/src/node/eejs/index.ts b/src/node/eejs/index.ts index b7f2cf998..5d57e4751 100644 --- a/src/node/eejs/index.ts +++ b/src/node/eejs/index.ts @@ -22,7 +22,7 @@ const ejs = require('ejs'); const fs = require('fs'); -const hooks = require('../../static/js/pluginfw/hooks.js'); +const hooks = require('../../static/js/pluginfw/hooks'); const path = require('path'); const resolve = require('resolve'); const settings = require('../utils/Settings'); diff --git a/src/node/handler/ImportHandler.ts b/src/node/handler/ImportHandler.ts index 330a2d6f9..286b4fb56 100644 --- a/src/node/handler/ImportHandler.ts +++ b/src/node/handler/ImportHandler.ts @@ -31,7 +31,7 @@ import os from 'os'; const importHtml = require('../utils/ImportHtml'); const importEtherpad = require('../utils/ImportEtherpad'); import log4js from 'log4js'; -const hooks = require('../../static/js/pluginfw/hooks.js'); +const hooks = require('../../static/js/pluginfw/hooks'); const logger = log4js.getLogger('ImportHandler'); diff --git a/src/node/handler/PadMessageHandler.ts b/src/node/handler/PadMessageHandler.ts index 390949607..2976381bb 100644 --- a/src/node/handler/PadMessageHandler.ts +++ b/src/node/handler/PadMessageHandler.ts @@ -21,28 +21,29 @@ import {MapArrayType} from "../types/MapType"; -const AttributeMap = require('../../static/js/AttributeMap'); +import AttributeMap from '../../static/js/AttributeMap'; const padManager = require('../db/PadManager'); const Changeset = require('../../static/js/Changeset'); -const ChatMessage = require('../../static/js/ChatMessage'); -const AttributePool = require('../../static/js/AttributePool'); +import ChatMessage from '../../static/js/ChatMessage'; +import AttributePool from '../../static/js/AttributePool'; const AttributeManager = require('../../static/js/AttributeManager'); const authorManager = require('../db/AuthorManager'); -const {padutils} = require('../../static/js/pad_utils'); +import padutils from '../../static/js/pad_utils'; const readOnlyManager = require('../db/ReadOnlyManager'); const settings = require('../utils/Settings'); const securityManager = require('../db/SecurityManager'); -const plugins = require('../../static/js/pluginfw/plugin_defs.js'); +const plugins = require('../../static/js/pluginfw/plugin_defs'); import log4js from 'log4js'; const messageLogger = log4js.getLogger('message'); const accessLogger = log4js.getLogger('access'); -const hooks = require('../../static/js/pluginfw/hooks.js'); +const hooks = require('../../static/js/pluginfw/hooks'); const stats = require('../stats') const assert = require('assert').strict; import {RateLimiterMemory} from 'rate-limiter-flexible'; import {ChangesetRequest, PadUserInfo, SocketClientRequest} from "../types/SocketClientRequest"; import {APool, AText, PadAuthor, PadType} from "../types/PadType"; import {ChangeSet} from "../types/ChangeSet"; +import {ChatMessageMessage, ClientReadyMessage, ClientSaveRevisionMessage, ClientSuggestUserName, ClientUserChangesMessage, ClientVarMessage, CustomMessage, UserNewInfoMessage} from "../../static/js/types/SocketIOMessage"; const webaccess = require('../hooks/express/webaccess'); const { checkValidRev } = require('../utils/checkValidRev'); @@ -214,7 +215,7 @@ exports.handleDisconnect = async (socket:any) => { * @param socket the socket.io Socket object for the client * @param message the message from the client */ -exports.handleMessage = async (socket:any, message:typeof ChatMessage) => { +exports.handleMessage = async (socket:any, message: ClientVarMessage) => { const env = process.env.NODE_ENV || 'development'; if (env === 'production') { @@ -348,15 +349,15 @@ exports.handleMessage = async (socket:any, message:typeof ChatMessage) => { stats.counter('pendingEdits').inc(); await padChannels.enqueue(thisSession.padId, {socket, message}); break; - case 'USERINFO_UPDATE': await handleUserInfoUpdate(socket, message); break; - case 'CHAT_MESSAGE': await handleChatMessage(socket, message); break; + case 'USERINFO_UPDATE': await handleUserInfoUpdate(socket, message as unknown as UserNewInfoMessage); break; + case 'CHAT_MESSAGE': await handleChatMessage(socket, message as unknown as ChatMessageMessage); break; case 'GET_CHAT_MESSAGES': await handleGetChatMessages(socket, message); break; - case 'SAVE_REVISION': await handleSaveRevisionMessage(socket, message); break; + case 'SAVE_REVISION': await handleSaveRevisionMessage(socket, message as unknown as ClientSaveRevisionMessage); break; case 'CLIENT_MESSAGE': { const {type} = message.data.payload; try { switch (type) { - case 'suggestUserName': handleSuggestUserName(socket, message); break; + case 'suggestUserName': handleSuggestUserName(socket, message as unknown as ClientSuggestUserName); break; default: throw new Error('unknown message type'); } } catch (err) { @@ -384,7 +385,7 @@ exports.handleMessage = async (socket:any, message:typeof ChatMessage) => { * @param socket the socket.io Socket object for the client * @param message the message from the client */ -const handleSaveRevisionMessage = async (socket:any, message: string) => { +const handleSaveRevisionMessage = async (socket:any, message: ClientSaveRevisionMessage) => { const {padId, author: authorId} = sessioninfos[socket.id]; const pad = await padManager.getPad(padId, null, authorId); await pad.addSavedRevision(pad.head, authorId); @@ -397,7 +398,7 @@ const handleSaveRevisionMessage = async (socket:any, message: string) => { * @param msg {Object} the message we're sending * @param sessionID {string} the socketIO session to which we're sending this message */ -exports.handleCustomObjectMessage = (msg: typeof ChatMessage, sessionID: string) => { +exports.handleCustomObjectMessage = (msg: CustomMessage, sessionID: string) => { if (msg.data.type === 'CUSTOM') { if (sessionID) { // a sessionID is targeted: directly to this sessionID @@ -432,7 +433,7 @@ exports.handleCustomMessage = (padID: string, msgString:string) => { * @param socket the socket.io Socket object for the client * @param message the message from the client */ -const handleChatMessage = async (socket:any, message: typeof ChatMessage) => { +const handleChatMessage = async (socket:any, message: ChatMessageMessage) => { const chatMessage = ChatMessage.fromObject(message.data.message); const {padId, author: authorId} = sessioninfos[socket.id]; // Don't trust the user-supplied values. @@ -452,7 +453,7 @@ const handleChatMessage = async (socket:any, message: typeof ChatMessage) => { * @param {string} [padId] - The destination pad ID. Deprecated; pass a chat message * object as the first argument and the destination pad ID as the second argument instead. */ -exports.sendChatMessageToPadClients = async (mt: typeof ChatMessage|number, puId: string, text:string|null = null, padId:string|null = null) => { +exports.sendChatMessageToPadClients = async (mt: ChatMessage|number, puId: string, text:string|null = null, padId:string|null = null) => { const message = mt instanceof ChatMessage ? mt : new ChatMessage(text, puId, mt); padId = mt instanceof ChatMessage ? puId : padId; const pad = await padManager.getPad(padId, null, message.authorId); @@ -499,7 +500,7 @@ const handleGetChatMessages = async (socket:any, {data: {start, end}}:any) => { * @param socket the socket.io Socket object for the client * @param message the message from the client */ -const handleSuggestUserName = (socket:any, message: typeof ChatMessage) => { +const handleSuggestUserName = (socket:any, message: ClientSuggestUserName) => { const {newName, unnamedId} = message.data.payload; if (newName == null) throw new Error('missing newName'); if (unnamedId == null) throw new Error('missing unnamedId'); @@ -519,7 +520,7 @@ const handleSuggestUserName = (socket:any, message: typeof ChatMessage) => { * @param socket the socket.io Socket object for the client * @param message the message from the client */ -const handleUserInfoUpdate = async (socket:any, {data: {userInfo: {name, colorId}}}: PadUserInfo) => { +const handleUserInfoUpdate = async (socket:any, {data: {userInfo: {name, colorId}}}: UserNewInfoMessage) => { if (colorId == null) throw new Error('missing colorId'); if (!name) name = null; const session = sessioninfos[socket.id]; @@ -567,7 +568,9 @@ const handleUserInfoUpdate = async (socket:any, {data: {userInfo: {name, colorId * @param socket the socket.io Socket object for the client * @param message the message from the client */ -const handleUserChanges = async (socket:any, message: typeof ChatMessage) => { +const handleUserChanges = async (socket:any, message: { + data: ClientUserChangesMessage +}) => { // This one's no longer pending, as we're gonna process it now stats.counter('pendingEdits').dec(); @@ -738,7 +741,7 @@ exports.updatePadClients = async (pad: PadType) => { /** * Copied from the Etherpad Source Code. Don't know what this method does excatly... */ -const _correctMarkersInPad = (atext: AText, apool: APool) => { +const _correctMarkersInPad = (atext: AText, apool: AttributePool) => { const text = atext.text; // collect char positions of line markers (e.g. bullets) in new atext @@ -785,7 +788,7 @@ const _correctMarkersInPad = (atext: AText, apool: APool) => { * @param socket the socket.io Socket object for the client * @param message the message from the client */ -const handleClientReady = async (socket:any, message: typeof ChatMessage) => { +const handleClientReady = async (socket:any, message: ClientReadyMessage) => { const sessionInfo = sessioninfos[socket.id]; if (sessionInfo == null) throw new Error('client disconnected'); assert(sessionInfo.author); @@ -793,8 +796,9 @@ const handleClientReady = async (socket:any, message: typeof ChatMessage) => { await hooks.aCallAll('clientReady', message); // Deprecated due to awkward context. let {colorId: authorColorId, name: authorName} = message.userInfo || {}; - if (authorColorId && !/^#(?:[0-9A-F]{3}){1,2}$/i.test(authorColorId)) { + if (authorColorId && !/^#(?:[0-9A-F]{3}){1,2}$/i.test(authorColorId as string)) { messageLogger.warn(`Ignoring invalid colorId in CLIENT_READY message: ${authorColorId}`); + // @ts-ignore authorColorId = null; } await Promise.all([ @@ -872,7 +876,7 @@ const handleClientReady = async (socket:any, message: typeof ChatMessage) => { const revisionsNeeded = []; const changesets:MapArrayType = {}; - let startNum = message.client_rev + 1; + let startNum = message.client_rev! + 1; let endNum = pad.getHeadRevisionNumber() + 1; const headNum = pad.getHeadRevisionNumber(); diff --git a/src/node/hooks/i18n.ts b/src/node/hooks/i18n.ts index 500f1f887..c4cc58bdd 100644 --- a/src/node/hooks/i18n.ts +++ b/src/node/hooks/i18n.ts @@ -7,7 +7,7 @@ const languages = require('languages4translatewiki'); const fs = require('fs'); const path = require('path'); const _ = require('underscore'); -const pluginDefs = require('../../static/js/pluginfw/plugin_defs.js'); +const pluginDefs = require('../../static/js/pluginfw/plugin_defs'); const existsSync = require('../utils/path_exists'); const settings = require('../utils/Settings'); diff --git a/src/node/utils/ExportHelper.ts b/src/node/utils/ExportHelper.ts index f3a438e86..e12332b06 100644 --- a/src/node/utils/ExportHelper.ts +++ b/src/node/utils/ExportHelper.ts @@ -19,7 +19,8 @@ * limitations under the License. */ -const AttributeMap = require('../../static/js/AttributeMap'); +import AttributeMap from '../../static/js/AttributeMap'; +import AttributePool from "../../static/js/AttributePool"; const Changeset = require('../../static/js/Changeset'); const { checkValidRev } = require('./checkValidRev'); @@ -51,7 +52,7 @@ type LineModel = { [id:string]:string|number|LineModel } -exports._analyzeLine = (text:string, aline: LineModel, apool: Function) => { +exports._analyzeLine = (text:string, aline: LineModel, apool: AttributePool) => { const line: LineModel = {}; // identify list diff --git a/src/node/utils/ExportHtml.ts b/src/node/utils/ExportHtml.ts index 3b84c4380..138f084ba 100644 --- a/src/node/utils/ExportHtml.ts +++ b/src/node/utils/ExportHtml.ts @@ -27,7 +27,7 @@ const hooks = require('../../static/js/pluginfw/hooks'); const eejs = require('../eejs'); const _analyzeLine = require('./ExportHelper')._analyzeLine; const _encodeWhitespace = require('./ExportHelper')._encodeWhitespace; -const padutils = require('../../static/js/pad_utils').padutils; +import padutils from "../../static/js/pad_utils"; const getPadHTML = async (pad: PadType, revNum: string) => { let atext = pad.atext; diff --git a/src/node/utils/ImportEtherpad.ts b/src/node/utils/ImportEtherpad.ts index e75c5fd42..cf34107c7 100644 --- a/src/node/utils/ImportEtherpad.ts +++ b/src/node/utils/ImportEtherpad.ts @@ -18,7 +18,7 @@ import {APool} from "../types/PadType"; * limitations under the License. */ -const AttributePool = require('../../static/js/AttributePool'); +import AttributePool from '../../static/js/AttributePool'; const {Pad} = require('../db/Pad'); const Stream = require('./Stream'); const authorManager = require('../db/AuthorManager'); @@ -61,7 +61,7 @@ exports.setPadRaw = async (padId: string, r: string, authorId = '') => { try { const processRecord = async (key:string, value: null|{ padIDs: string|Record, - pool: APool + pool: AttributePool }) => { if (!value) return; const keyParts = key.split(':'); diff --git a/src/node/utils/padDiff.ts b/src/node/utils/padDiff.ts index d731ebbe4..6074f8bbc 100644 --- a/src/node/utils/padDiff.ts +++ b/src/node/utils/padDiff.ts @@ -3,7 +3,7 @@ import {PadAuthor, PadType} from "../types/PadType"; import {MapArrayType} from "../types/MapType"; -const AttributeMap = require('../../static/js/AttributeMap'); +import AttributeMap from '../../static/js/AttributeMap'; const Changeset = require('../../static/js/Changeset'); const attributes = require('../../static/js/attributes'); const exportHtml = require('./ExportHtml'); diff --git a/src/package.json b/src/package.json index ce80bd88e..80031af7a 100644 --- a/src/package.json +++ b/src/package.json @@ -86,6 +86,7 @@ "@types/formidable": "^3.4.5", "@types/http-errors": "^2.0.4", "@types/jquery": "^3.5.30", + "@types/js-cookie": "^3.0.6", "@types/jsdom": "^21.1.7", "@types/jsonwebtoken": "^9.0.6", "@types/mime-types": "^2.1.4", diff --git a/src/static/js/AttributeManager.js b/src/static/js/AttributeManager.ts similarity index 99% rename from src/static/js/AttributeManager.js rename to src/static/js/AttributeManager.ts index 63af431d9..6a0a545b4 100644 --- a/src/static/js/AttributeManager.js +++ b/src/static/js/AttributeManager.ts @@ -1,6 +1,7 @@ +// @ts-nocheck 'use strict'; -const AttributeMap = require('./AttributeMap'); +import AttributeMap from './AttributeMap'; const Changeset = require('./Changeset'); const ChangesetUtils = require('./ChangesetUtils'); const attributes = require('./attributes'); diff --git a/src/static/js/AttributeMap.js b/src/static/js/AttributeMap.ts similarity index 78% rename from src/static/js/AttributeMap.js rename to src/static/js/AttributeMap.ts index 55640eb8b..07bc106a5 100644 --- a/src/static/js/AttributeMap.js +++ b/src/static/js/AttributeMap.ts @@ -1,6 +1,9 @@ 'use strict'; -const attributes = require('./attributes'); +import AttributePool from "./AttributePool"; +import {Attribute} from "./types/Attribute"; + +import attributes from './attributes'; /** * A `[key, value]` pair of strings describing a text attribute. @@ -21,6 +24,7 @@ const attributes = require('./attributes'); * Convenience class to convert an Op's attribute string to/from a Map of key, value pairs. */ class AttributeMap extends Map { + private readonly pool? : AttributePool|null /** * Converts an attribute string into an AttributeMap. * @@ -28,14 +32,14 @@ class AttributeMap extends Map { * @param {AttributePool} pool - Attribute pool. * @returns {AttributeMap} */ - static fromString(str, pool) { + public static fromString(str: string, pool?: AttributePool|null): AttributeMap { return new AttributeMap(pool).updateFromString(str); } /** * @param {AttributePool} pool - Attribute pool. */ - constructor(pool) { + constructor(pool?: AttributePool|null) { super(); /** @public */ this.pool = pool; @@ -46,15 +50,15 @@ class AttributeMap extends Map { * @param {string} v - Attribute value. * @returns {AttributeMap} `this` (for chaining). */ - set(k, v) { + set(k: string, v: string):this { k = k == null ? '' : String(k); v = v == null ? '' : String(v); - this.pool.putAttrib([k, v]); + this.pool!.putAttrib([k, v]); return super.set(k, v); } toString() { - return attributes.attribsToString(attributes.sort([...this]), this.pool); + return attributes.attribsToString(attributes.sort([...this]), this.pool!); } /** @@ -63,7 +67,7 @@ class AttributeMap extends Map { * key is removed from this map (if present). * @returns {AttributeMap} `this` (for chaining). */ - update(entries, emptyValueIsDelete = false) { + update(entries: Iterable, emptyValueIsDelete: boolean = false): AttributeMap { for (let [k, v] of entries) { k = k == null ? '' : String(k); v = v == null ? '' : String(v); @@ -83,9 +87,9 @@ class AttributeMap extends Map { * key is removed from this map (if present). * @returns {AttributeMap} `this` (for chaining). */ - updateFromString(str, emptyValueIsDelete = false) { - return this.update(attributes.attribsFromString(str, this.pool), emptyValueIsDelete); + updateFromString(str: string, emptyValueIsDelete: boolean = false): AttributeMap { + return this.update(attributes.attribsFromString(str, this.pool!), emptyValueIsDelete); } } -module.exports = AttributeMap; +export default AttributeMap diff --git a/src/static/js/AttributePool.js b/src/static/js/AttributePool.ts similarity index 91% rename from src/static/js/AttributePool.js rename to src/static/js/AttributePool.ts index ccdd2eb35..5bbe52122 100644 --- a/src/static/js/AttributePool.js +++ b/src/static/js/AttributePool.ts @@ -44,6 +44,8 @@ * @property {number} nextNum - The attribute ID to assign to the next new attribute. */ +import {Attribute} from "./types/Attribute"; + /** * Represents an attribute pool, which is a collection of attributes (pairs of key and value * strings) along with their identifiers (non-negative integers). @@ -55,6 +57,14 @@ * in the pad. */ class AttributePool { + numToAttrib: { + [key: number]: [string, string] + } + private attribToNum: { + [key: number]: [string, string] + } + private nextNum: number + constructor() { /** * Maps an attribute identifier to the attribute's `[key, value]` string pair. @@ -96,7 +106,10 @@ class AttributePool { */ clone() { const c = new AttributePool(); - for (const [n, a] of Object.entries(this.numToAttrib)) c.numToAttrib[n] = [a[0], a[1]]; + for (const [n, a] of Object.entries(this.numToAttrib)){ + // @ts-ignore + c.numToAttrib[n] = [a[0], a[1]]; + } Object.assign(c.attribToNum, this.attribToNum); c.nextNum = this.nextNum; return c; @@ -111,15 +124,17 @@ class AttributePool { * membership in the pool without mutating the pool. * @returns {number} The attribute's identifier, or -1 if the attribute is not in the pool. */ - putAttrib(attrib, dontAddIfAbsent = false) { + putAttrib(attrib: Attribute, dontAddIfAbsent = false) { const str = String(attrib); if (str in this.attribToNum) { + // @ts-ignore return this.attribToNum[str]; } if (dontAddIfAbsent) { return -1; } const num = this.nextNum++; + // @ts-ignore this.attribToNum[str] = num; this.numToAttrib[num] = [String(attrib[0] || ''), String(attrib[1] || '')]; return num; @@ -130,7 +145,7 @@ class AttributePool { * @returns {Attribute} The attribute with the given identifier, or nullish if there is no such * attribute. */ - getAttrib(num) { + getAttrib(num: number): Attribute { const pair = this.numToAttrib[num]; if (!pair) { return pair; @@ -143,7 +158,7 @@ class AttributePool { * @returns {string} Eqivalent to `getAttrib(num)[0]` if the attribute exists, otherwise the empty * string. */ - getAttribKey(num) { + getAttribKey(num: number): string { const pair = this.numToAttrib[num]; if (!pair) return ''; return pair[0]; @@ -154,7 +169,7 @@ class AttributePool { * @returns {string} Eqivalent to `getAttrib(num)[1]` if the attribute exists, otherwise the empty * string. */ - getAttribValue(num) { + getAttribValue(num: number) { const pair = this.numToAttrib[num]; if (!pair) return ''; return pair[1]; @@ -166,8 +181,8 @@ class AttributePool { * @param {Function} func - Callback to call with two arguments: key and value. Its return value * is ignored. */ - eachAttrib(func) { - for (const n of Object.keys(this.numToAttrib)) { + eachAttrib(func: (k: string, v: string)=>void) { + for (const n in this.numToAttrib) { const pair = this.numToAttrib[n]; func(pair[0], pair[1]); } @@ -196,11 +211,12 @@ class AttributePool { * `new AttributePool().fromJsonable(pool.toJsonable())` to copy because the resulting shared * state will lead to pool corruption. */ - fromJsonable(obj) { + fromJsonable(obj: this) { this.numToAttrib = obj.numToAttrib; this.nextNum = obj.nextNum; this.attribToNum = {}; for (const n of Object.keys(this.numToAttrib)) { + // @ts-ignore this.attribToNum[String(this.numToAttrib[n])] = Number(n); } return this; @@ -213,6 +229,7 @@ class AttributePool { if (!Number.isInteger(this.nextNum)) throw new Error('nextNum property is not an integer'); if (this.nextNum < 0) throw new Error('nextNum property is negative'); for (const prop of ['numToAttrib', 'attribToNum']) { + // @ts-ignore const obj = this[prop]; if (obj == null) throw new Error(`${prop} property is null`); if (typeof obj !== 'object') throw new TypeError(`${prop} property is not an object`); @@ -231,9 +248,10 @@ class AttributePool { if (v == null) throw new TypeError(`attrib ${i} value is null`); if (typeof v !== 'string') throw new TypeError(`attrib ${i} value is not a string`); const attrStr = String(attr); + // @ts-ignore if (this.attribToNum[attrStr] !== i) throw new Error(`attribToNum for ${attrStr} !== ${i}`); } } } -module.exports = AttributePool; +export default AttributePool diff --git a/src/static/js/Changeset.js b/src/static/js/Changeset.ts similarity index 99% rename from src/static/js/Changeset.js rename to src/static/js/Changeset.ts index 53b3f2c8f..99987f8a8 100644 --- a/src/static/js/Changeset.js +++ b/src/static/js/Changeset.ts @@ -1,3 +1,4 @@ +// @ts-nocheck 'use strict'; /* @@ -22,10 +23,10 @@ * https://github.com/ether/pad/blob/master/infrastructure/ace/www/easysync2.js */ -const AttributeMap = require('./AttributeMap'); -const AttributePool = require('./AttributePool'); +import AttributeMap from './AttributeMap'; +import AttributePool from './AttributePool'; const attributes = require('./attributes'); -const {padutils} = require('./pad_utils'); +import padutils from './pad_utils'; /** * A `[key, value]` pair of strings describing a text attribute. diff --git a/src/static/js/ChangesetUtils.js b/src/static/js/ChangesetUtils.ts similarity index 99% rename from src/static/js/ChangesetUtils.js rename to src/static/js/ChangesetUtils.ts index ef2be2ebe..b42a7e8f9 100644 --- a/src/static/js/ChangesetUtils.js +++ b/src/static/js/ChangesetUtils.ts @@ -1,3 +1,4 @@ +// @ts-nocheck 'use strict'; /** diff --git a/src/static/js/ChatMessage.js b/src/static/js/ChatMessage.ts similarity index 66% rename from src/static/js/ChatMessage.js rename to src/static/js/ChatMessage.ts index a627f88f9..db2d3403a 100644 --- a/src/static/js/ChatMessage.js +++ b/src/static/js/ChatMessage.ts @@ -1,6 +1,6 @@ 'use strict'; -const {padutils: {warnDeprecated}} = require('./pad_utils'); +import padUtils from './pad_utils' /** * Represents a chat message stored in the database and transmitted among users. Plugins can extend @@ -8,14 +8,25 @@ const {padutils: {warnDeprecated}} = require('./pad_utils'); * * Supports serialization to JSON. */ -class ChatMessage { - static fromObject(obj) { +export class ChatMessage { + customMetadata: any + text: string|null + public authorId: string|null + displayName: string|null + time: number|null + static fromObject(obj: ChatMessage) { // The userId property was renamed to authorId, and userName was renamed to displayName. Accept // the old names in case the db record was written by an older version of Etherpad. obj = Object.assign({}, obj); // Don't mutate the caller's object. - if ('userId' in obj && !('authorId' in obj)) obj.authorId = obj.userId; + if ('userId' in obj && !('authorId' in obj)) { // @ts-ignore + obj.authorId = obj.userId; + } + // @ts-ignore delete obj.userId; - if ('userName' in obj && !('displayName' in obj)) obj.displayName = obj.userName; + if ('userName' in obj && !('displayName' in obj)) { // @ts-ignore + obj.displayName = obj.userName; + } + // @ts-ignore delete obj.userName; return Object.assign(new ChatMessage(), obj); } @@ -25,7 +36,7 @@ class ChatMessage { * @param {?string} [authorId] - Initial value of the `authorId` property. * @param {?number} [time] - Initial value of the `time` property. */ - constructor(text = null, authorId = null, time = null) { + constructor(text: string | null = null, authorId: string | null = null, time: number | null = null) { /** * The raw text of the user's chat message (before any rendering or processing). * @@ -62,11 +73,11 @@ class ChatMessage { * @type {string} */ get userId() { - warnDeprecated('ChatMessage.userId property is deprecated; use .authorId instead'); + padUtils.warnDeprecated('ChatMessage.userId property is deprecated; use .authorId instead'); return this.authorId; } set userId(val) { - warnDeprecated('ChatMessage.userId property is deprecated; use .authorId instead'); + padUtils.warnDeprecated('ChatMessage.userId property is deprecated; use .authorId instead'); this.authorId = val; } @@ -77,11 +88,11 @@ class ChatMessage { * @type {string} */ get userName() { - warnDeprecated('ChatMessage.userName property is deprecated; use .displayName instead'); + padUtils.warnDeprecated('ChatMessage.userName property is deprecated; use .displayName instead'); return this.displayName; } set userName(val) { - warnDeprecated('ChatMessage.userName property is deprecated; use .displayName instead'); + padUtils.warnDeprecated('ChatMessage.userName property is deprecated; use .displayName instead'); this.displayName = val; } @@ -89,10 +100,12 @@ class ChatMessage { // doesn't support authorId and displayName. toJSON() { const {authorId, displayName, ...obj} = this; + // @ts-ignore obj.userId = authorId; + // @ts-ignore obj.userName = displayName; return obj; } } -module.exports = ChatMessage; +export default ChatMessage diff --git a/src/static/js/ace.js b/src/static/js/ace.ts similarity index 99% rename from src/static/js/ace.js rename to src/static/js/ace.ts index a1b5d99c8..4c062584c 100644 --- a/src/static/js/ace.js +++ b/src/static/js/ace.ts @@ -1,3 +1,4 @@ +// @ts-nocheck 'use strict'; /** * This code is mostly from the old Etherpad. Please help us to comment this code. diff --git a/src/static/js/ace2_common.js b/src/static/js/ace2_common.ts similarity index 76% rename from src/static/js/ace2_common.js rename to src/static/js/ace2_common.ts index c1dab5cfd..0a5f308e6 100644 --- a/src/static/js/ace2_common.js +++ b/src/static/js/ace2_common.ts @@ -6,6 +6,8 @@ * TL;DR COMMENTS ON THIS FILE ARE HIGHLY APPRECIATED */ +import {MapArrayType} from "../../node/types/MapType"; + /** * Copyright 2009 Google Inc. * @@ -22,11 +24,13 @@ * limitations under the License. */ -const isNodeText = (node) => (node.nodeType === 3); +export const isNodeText = (node: { + nodeType: number +}) => (node.nodeType === 3); -const getAssoc = (obj, name) => obj[`_magicdom_${name}`]; +export const getAssoc = (obj: MapArrayType, name: string) => obj[`_magicdom_${name}`]; -const setAssoc = (obj, name, value) => { +export const setAssoc = (obj: MapArrayType, name: string, value: string) => { // note that in IE designMode, properties of a node can get // copied to new nodes that are spawned during editing; also, // properties representable in HTML text can survive copy-and-paste @@ -38,7 +42,7 @@ const setAssoc = (obj, name, value) => { // between false and true, a number between 0 and numItems inclusive. -const binarySearch = (numItems, func) => { +export const binarySearch = (numItems: number, func: (num: number)=>boolean) => { if (numItems < 1) return 0; if (func(0)) return 0; if (!func(numItems - 1)) return numItems; @@ -52,17 +56,10 @@ const binarySearch = (numItems, func) => { return high; }; -const binarySearchInfinite = (expectedLength, func) => { +export const binarySearchInfinite = (expectedLength: number, func: (num: number)=>boolean) => { let i = 0; while (!func(i)) i += expectedLength; return binarySearch(i, func); }; -const noop = () => {}; - -exports.isNodeText = isNodeText; -exports.getAssoc = getAssoc; -exports.setAssoc = setAssoc; -exports.binarySearch = binarySearch; -exports.binarySearchInfinite = binarySearchInfinite; -exports.noop = noop; +export const noop = () => {}; diff --git a/src/static/js/ace2_inner.js b/src/static/js/ace2_inner.ts similarity index 99% rename from src/static/js/ace2_inner.js rename to src/static/js/ace2_inner.ts index 641c5ecdb..af391f115 100644 --- a/src/static/js/ace2_inner.js +++ b/src/static/js/ace2_inner.ts @@ -1,3 +1,4 @@ +// @ts-nocheck 'use strict'; /** @@ -18,9 +19,9 @@ */ let documentAttributeManager; -const AttributeMap = require('./AttributeMap'); +import AttributeMap from './AttributeMap'; const browser = require('./vendors/browser'); -const padutils = require('./pad_utils').padutils; +import padutils from './pad_utils' const Ace2Common = require('./ace2_common'); const $ = require('./rjquery').$; @@ -29,19 +30,18 @@ const getAssoc = Ace2Common.getAssoc; const setAssoc = Ace2Common.setAssoc; const noop = Ace2Common.noop; const hooks = require('./pluginfw/hooks'); - +import SkipList from "./skiplist"; import Scroll from './scroll' +import AttribPool from './AttributePool' function Ace2Inner(editorInfo, cssManagers) { const makeChangesetTracker = require('./changesettracker').makeChangesetTracker; const colorutils = require('./colorutils').colorutils; const makeContentCollector = require('./contentcollector').makeContentCollector; const domline = require('./domline').domline; - const AttribPool = require('./AttributePool'); const Changeset = require('./Changeset'); const ChangesetUtils = require('./ChangesetUtils'); const linestylefilter = require('./linestylefilter').linestylefilter; - const SkipList = require('./skiplist'); const undoModule = require('./undomodule').undoModule; const AttributeManager = require('./AttributeManager'); const DEBUG = false; diff --git a/src/static/js/attributes.js b/src/static/js/attributes.ts similarity index 77% rename from src/static/js/attributes.js rename to src/static/js/attributes.ts index 4ab347019..b164f8759 100644 --- a/src/static/js/attributes.js +++ b/src/static/js/attributes.ts @@ -17,6 +17,9 @@ * @typedef {string} AttributeString */ +import AttributePool from "./AttributePool"; +import {Attribute} from "./types/Attribute"; + /** * Converts an attribute string into a sequence of attribute identifier numbers. * @@ -28,7 +31,7 @@ * appear in `str`. * @returns {Generator} */ -exports.decodeAttribString = function* (str) { +export const decodeAttribString = function* (str: string): Generator { const re = /\*([0-9a-z]+)|./gy; let match; while ((match = re.exec(str)) != null) { @@ -38,7 +41,7 @@ exports.decodeAttribString = function* (str) { } }; -const checkAttribNum = (n) => { +const checkAttribNum = (n: number|object) => { if (typeof n !== 'number') throw new TypeError(`not a number: ${n}`); if (n < 0) throw new Error(`attribute number is negative: ${n}`); if (n !== Math.trunc(n)) throw new Error(`attribute number is not an integer: ${n}`); @@ -50,7 +53,7 @@ const checkAttribNum = (n) => { * @param {Iterable} attribNums - Sequence of attribute numbers. * @returns {AttributeString} */ -exports.encodeAttribString = (attribNums) => { +export const encodeAttribString = (attribNums: Iterable): string => { let str = ''; for (const n of attribNums) { checkAttribNum(n); @@ -67,7 +70,7 @@ exports.encodeAttribString = (attribNums) => { * @yields {Attribute} The identified attributes, in the same order as `attribNums`. * @returns {Generator} */ -exports.attribsFromNums = function* (attribNums, pool) { +export const attribsFromNums = function* (attribNums: Iterable, pool: AttributePool): Generator { for (const n of attribNums) { checkAttribNum(n); const attrib = pool.getAttrib(n); @@ -87,7 +90,7 @@ exports.attribsFromNums = function* (attribNums, pool) { * @yields {number} The attribute number of each attribute in `attribs`, in order. * @returns {Generator} */ -exports.attribsToNums = function* (attribs, pool) { +export const attribsToNums = function* (attribs: Iterable, pool: AttributePool) { for (const attrib of attribs) yield pool.putAttrib(attrib); }; @@ -102,8 +105,8 @@ exports.attribsToNums = function* (attribs, pool) { * @yields {Attribute} The attributes identified in `str`, in order. * @returns {Generator} */ -exports.attribsFromString = function* (str, pool) { - yield* exports.attribsFromNums(exports.decodeAttribString(str), pool); +export const attribsFromString = function* (str: string, pool: AttributePool): Generator { + yield* attribsFromNums(decodeAttribString(str), pool); }; /** @@ -116,8 +119,8 @@ exports.attribsFromString = function* (str, pool) { * @param {AttributePool} pool - Attribute pool. * @returns {AttributeString} */ -exports.attribsToString = - (attribs, pool) => exports.encodeAttribString(exports.attribsToNums(attribs, pool)); +export const attribsToString = + (attribs: Iterable, pool: AttributePool): string => encodeAttribString(attribsToNums(attribs, pool)); /** * Sorts the attributes in canonical order. The order of entries with the same attribute name is @@ -126,5 +129,14 @@ exports.attribsToString = * @param {Attribute[]} attribs - Attributes to sort in place. * @returns {Attribute[]} `attribs` (for chaining). */ -exports.sort = - (attribs) => attribs.sort(([keyA], [keyB]) => (keyA > keyB ? 1 : 0) - (keyA < keyB ? 1 : 0)); +export const sort = (attribs: Attribute[]): Attribute[] => attribs.sort(([keyA], [keyB]) => (keyA > keyB ? 1 : 0) - (keyA < keyB ? 1 : 0)); + +export default { + decodeAttribString, + encodeAttribString, + attribsFromNums, + attribsToNums, + attribsFromString, + attribsToString, + sort, +} diff --git a/src/static/js/basic_error_handler.js b/src/static/js/basic_error_handler.ts similarity index 99% rename from src/static/js/basic_error_handler.js rename to src/static/js/basic_error_handler.ts index ab400aa8a..ce8c47353 100644 --- a/src/static/js/basic_error_handler.js +++ b/src/static/js/basic_error_handler.ts @@ -1,3 +1,4 @@ +// @ts-nocheck // @license magnet:?xt=urn:btih:8e4f440f4c65981c5bf93c76d35135ba5064d8b7&dn=apache-2.0.txt Apache-2.0 /* Copyright 2021 Richard Hansen */ diff --git a/src/static/js/broadcast.js b/src/static/js/broadcast.ts similarity index 99% rename from src/static/js/broadcast.js rename to src/static/js/broadcast.ts index 2163fd78e..518107e5a 100644 --- a/src/static/js/broadcast.js +++ b/src/static/js/broadcast.ts @@ -1,3 +1,4 @@ +// @ts-nocheck 'use strict'; /** @@ -24,7 +25,7 @@ const makeCSSManager = require('./cssmanager').makeCSSManager; const domline = require('./domline').domline; -const AttribPool = require('./AttributePool'); +import AttribPool from './AttributePool'; const Changeset = require('./Changeset'); const attributes = require('./attributes'); const linestylefilter = require('./linestylefilter').linestylefilter; diff --git a/src/static/js/broadcast_revisions.js b/src/static/js/broadcast_revisions.ts similarity index 99% rename from src/static/js/broadcast_revisions.js rename to src/static/js/broadcast_revisions.ts index 8a9eb2e6c..37272d860 100644 --- a/src/static/js/broadcast_revisions.js +++ b/src/static/js/broadcast_revisions.ts @@ -1,3 +1,4 @@ +// @ts-nocheck 'use strict'; /** diff --git a/src/static/js/broadcast_slider.js b/src/static/js/broadcast_slider.ts similarity index 99% rename from src/static/js/broadcast_slider.js rename to src/static/js/broadcast_slider.ts index 848ba06cf..ccd896c4c 100644 --- a/src/static/js/broadcast_slider.js +++ b/src/static/js/broadcast_slider.ts @@ -1,3 +1,4 @@ +// @ts-nocheck 'use strict'; /** * This code is mostly from the old Etherpad. Please help us to comment this code. diff --git a/src/static/js/changesettracker.js b/src/static/js/changesettracker.ts similarity index 98% rename from src/static/js/changesettracker.js rename to src/static/js/changesettracker.ts index 30c70aa74..86a63546b 100644 --- a/src/static/js/changesettracker.js +++ b/src/static/js/changesettracker.ts @@ -1,3 +1,4 @@ +// @ts-nocheck 'use strict'; /** @@ -22,8 +23,8 @@ * limitations under the License. */ -const AttributeMap = require('./AttributeMap'); -const AttributePool = require('./AttributePool'); +import AttributeMap from './AttributeMap'; +import AttributePool from './AttributePool'; const Changeset = require('./Changeset'); const makeChangesetTracker = (scheduler, apool, aceCallbacksProvider) => { diff --git a/src/static/js/chat.js b/src/static/js/chat.ts old mode 100755 new mode 100644 similarity index 99% rename from src/static/js/chat.js rename to src/static/js/chat.ts index d32a62c7a..6357663c4 --- a/src/static/js/chat.js +++ b/src/static/js/chat.ts @@ -1,3 +1,4 @@ +// @ts-nocheck 'use strict'; /** * Copyright 2009 Google Inc., 2011 Peter 'Pita' Martischka (Primary Technology Ltd) @@ -15,8 +16,8 @@ * limitations under the License. */ -const ChatMessage = require('./ChatMessage'); -const padutils = require('./pad_utils').padutils; +import ChatMessage from './ChatMessage'; +import padutils from './pad_utils' const padcookie = require('./pad_cookie').padcookie; const Tinycon = require('tinycon/tinycon'); const hooks = require('./pluginfw/hooks'); diff --git a/src/static/js/collab_client.js b/src/static/js/collab_client.ts similarity index 99% rename from src/static/js/collab_client.js rename to src/static/js/collab_client.ts index 69c8e41f3..7bc1c37df 100644 --- a/src/static/js/collab_client.js +++ b/src/static/js/collab_client.ts @@ -1,3 +1,4 @@ +// @ts-nocheck 'use strict'; /** diff --git a/src/static/js/colorutils.js b/src/static/js/colorutils.ts similarity index 99% rename from src/static/js/colorutils.js rename to src/static/js/colorutils.ts index 9688b8e59..b60b32aa9 100644 --- a/src/static/js/colorutils.js +++ b/src/static/js/colorutils.ts @@ -1,3 +1,4 @@ +// @ts-nocheck 'use strict'; /** diff --git a/src/static/js/contentcollector.js b/src/static/js/contentcollector.ts similarity index 99% rename from src/static/js/contentcollector.js rename to src/static/js/contentcollector.ts index 4735374ee..0c67687e4 100644 --- a/src/static/js/contentcollector.js +++ b/src/static/js/contentcollector.ts @@ -1,3 +1,4 @@ +// @ts-nocheck 'use strict'; /** * This code is mostly from the old Etherpad. Please help us to comment this code. @@ -26,7 +27,7 @@ const _MAX_LIST_LEVEL = 16; -const AttributeMap = require('./AttributeMap'); +import AttributeMap from './AttributeMap'; const UNorm = require('unorm'); const Changeset = require('./Changeset'); const hooks = require('./pluginfw/hooks'); diff --git a/src/static/js/cssmanager.js b/src/static/js/cssmanager.ts similarity index 99% rename from src/static/js/cssmanager.js rename to src/static/js/cssmanager.ts index 5bf2adb30..89036df67 100644 --- a/src/static/js/cssmanager.js +++ b/src/static/js/cssmanager.ts @@ -1,3 +1,4 @@ +// @ts-nocheck 'use strict'; /** diff --git a/src/static/js/domline.js b/src/static/js/domline.ts similarity index 99% rename from src/static/js/domline.js rename to src/static/js/domline.ts index af786b2dc..900f60176 100644 --- a/src/static/js/domline.js +++ b/src/static/js/domline.ts @@ -1,3 +1,4 @@ +// @ts-nocheck 'use strict'; // THIS FILE IS ALSO AN APPJET MODULE: etherpad.collab.ace.domline diff --git a/src/static/js/linestylefilter.js b/src/static/js/linestylefilter.ts similarity index 99% rename from src/static/js/linestylefilter.js rename to src/static/js/linestylefilter.ts index 632e6b3cc..3caab50f0 100644 --- a/src/static/js/linestylefilter.js +++ b/src/static/js/linestylefilter.ts @@ -1,3 +1,4 @@ +// @ts-nocheck 'use strict'; /** @@ -35,7 +36,7 @@ const attributes = require('./attributes'); const hooks = require('./pluginfw/hooks'); const linestylefilter = {}; const AttributeManager = require('./AttributeManager'); -const padutils = require('./pad_utils').padutils; +import padutils from './pad_utils' linestylefilter.ATTRIB_CLASSES = { bold: 'tag:b', diff --git a/src/static/js/pad.js b/src/static/js/pad.ts similarity index 99% rename from src/static/js/pad.js rename to src/static/js/pad.ts index d6648f031..e94611fcd 100644 --- a/src/static/js/pad.js +++ b/src/static/js/pad.ts @@ -1,3 +1,4 @@ +// @ts-nocheck 'use strict'; /** @@ -33,7 +34,8 @@ require('./vendors/gritter'); import html10n from './vendors/html10n' -const Cookies = require('./pad_utils').Cookies; +import {Cookies} from "./pad_utils"; + const chat = require('./chat').chat; const getCollabClient = require('./collab_client').getCollabClient; const padconnectionstatus = require('./pad_connectionstatus').padconnectionstatus; @@ -44,9 +46,9 @@ const padimpexp = require('./pad_impexp').padimpexp; const padmodals = require('./pad_modals').padmodals; const padsavedrevs = require('./pad_savedrevs'); const paduserlist = require('./pad_userlist').paduserlist; -const padutils = require('./pad_utils').padutils; +import padutils from './pad_utils' const colorutils = require('./colorutils').colorutils; -const randomString = require('./pad_utils').randomString; +import {randomString} from "./pad_utils"; const socketio = require('./socketio'); const hooks = require('./pluginfw/hooks'); diff --git a/src/static/js/pad_automatic_reconnect.js b/src/static/js/pad_automatic_reconnect.ts similarity index 99% rename from src/static/js/pad_automatic_reconnect.js rename to src/static/js/pad_automatic_reconnect.ts index 03fc91432..8172d5be7 100644 --- a/src/static/js/pad_automatic_reconnect.js +++ b/src/static/js/pad_automatic_reconnect.ts @@ -1,3 +1,4 @@ +// @ts-nocheck 'use strict'; import html10n from './vendors/html10n'; diff --git a/src/static/js/pad_connectionstatus.js b/src/static/js/pad_connectionstatus.ts similarity index 99% rename from src/static/js/pad_connectionstatus.js rename to src/static/js/pad_connectionstatus.ts index 7b0497d96..600defa8d 100644 --- a/src/static/js/pad_connectionstatus.js +++ b/src/static/js/pad_connectionstatus.ts @@ -1,3 +1,4 @@ +// @ts-nocheck 'use strict'; /** diff --git a/src/static/js/pad_cookie.js b/src/static/js/pad_cookie.ts similarity index 97% rename from src/static/js/pad_cookie.js rename to src/static/js/pad_cookie.ts index 0e946ea5c..bc624e962 100644 --- a/src/static/js/pad_cookie.js +++ b/src/static/js/pad_cookie.ts @@ -1,3 +1,4 @@ +// @ts-nocheck 'use strict'; /** @@ -16,7 +17,7 @@ * limitations under the License. */ -const Cookies = require('./pad_utils').Cookies; +import {Cookies} from "./pad_utils"; exports.padcookie = new class { constructor() { diff --git a/src/static/js/pad_editbar.js b/src/static/js/pad_editbar.ts similarity index 99% rename from src/static/js/pad_editbar.js rename to src/static/js/pad_editbar.ts index af8d59f1f..d98174fe5 100644 --- a/src/static/js/pad_editbar.js +++ b/src/static/js/pad_editbar.ts @@ -1,3 +1,4 @@ +// @ts-nocheck 'use strict'; /** @@ -24,7 +25,7 @@ const browser = require('./vendors/browser'); const hooks = require('./pluginfw/hooks'); -const padutils = require('./pad_utils').padutils; +import padutils from "./pad_utils"; const padeditor = require('./pad_editor').padeditor; const padsavedrevs = require('./pad_savedrevs'); const _ = require('underscore'); diff --git a/src/static/js/pad_editor.js b/src/static/js/pad_editor.ts similarity index 98% rename from src/static/js/pad_editor.js rename to src/static/js/pad_editor.ts index 47a250734..362412f2f 100644 --- a/src/static/js/pad_editor.js +++ b/src/static/js/pad_editor.ts @@ -1,3 +1,4 @@ +// @ts-nocheck 'use strict'; /** * This code is mostly from the old Etherpad. Please help us to comment this code. @@ -21,9 +22,8 @@ * limitations under the License. */ -const Cookies = require('./pad_utils').Cookies; +import padutils,{Cookies} from "./pad_utils"; const padcookie = require('./pad_cookie').padcookie; -const padutils = require('./pad_utils').padutils; const Ace2Editor = require('./ace').Ace2Editor; import html10n from '../js/vendors/html10n' diff --git a/src/static/js/pad_impexp.js b/src/static/js/pad_impexp.ts similarity index 99% rename from src/static/js/pad_impexp.js rename to src/static/js/pad_impexp.ts index 3aca9fb7c..8b8575c81 100644 --- a/src/static/js/pad_impexp.js +++ b/src/static/js/pad_impexp.ts @@ -1,3 +1,4 @@ +// @ts-nocheck 'use strict'; /** diff --git a/src/static/js/pad_modals.js b/src/static/js/pad_modals.ts similarity index 99% rename from src/static/js/pad_modals.js rename to src/static/js/pad_modals.ts index 54bd83877..3e2c2459b 100644 --- a/src/static/js/pad_modals.js +++ b/src/static/js/pad_modals.ts @@ -1,3 +1,4 @@ +// @ts-nocheck 'use strict'; /** diff --git a/src/static/js/pad_savedrevs.js b/src/static/js/pad_savedrevs.ts similarity index 98% rename from src/static/js/pad_savedrevs.js rename to src/static/js/pad_savedrevs.ts index 4082e0380..6722a03a2 100644 --- a/src/static/js/pad_savedrevs.js +++ b/src/static/js/pad_savedrevs.ts @@ -1,3 +1,4 @@ +// @ts-nocheck 'use strict'; /** diff --git a/src/static/js/pad_userlist.js b/src/static/js/pad_userlist.ts similarity index 99% rename from src/static/js/pad_userlist.js rename to src/static/js/pad_userlist.ts index a0cbd4b44..227e6b063 100644 --- a/src/static/js/pad_userlist.js +++ b/src/static/js/pad_userlist.ts @@ -1,3 +1,4 @@ +// @ts-nocheck 'use strict'; /** @@ -16,7 +17,7 @@ * limitations under the License. */ -const padutils = require('./pad_utils').padutils; +import padutils from './pad_utils' const hooks = require('./pluginfw/hooks'); import html10n from './vendors/html10n'; let myUserInfo = {}; diff --git a/src/static/js/pad_utils.js b/src/static/js/pad_utils.ts similarity index 58% rename from src/static/js/pad_utils.js rename to src/static/js/pad_utils.ts index 467a8adc9..0a66abf00 100644 --- a/src/static/js/pad_utils.js +++ b/src/static/js/pad_utils.ts @@ -6,6 +6,8 @@ * TL;DR COMMENTS ON THIS FILE ARE HIGHLY APPRECIATED */ +import {binarySearch} from "./ace2_common"; + /** * Copyright 2009 Google Inc. * @@ -22,13 +24,14 @@ * limitations under the License. */ -const Security = require('./security'); +const Security = require('security'); +import jsCookie, {CookiesStatic} from 'js-cookie' /** * Generates a random String with the given length. Is needed to generate the Author, Group, * readonly, session Ids */ -const randomString = (len) => { +export const randomString = (len?: number) => { const chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'; let randomstring = ''; len = len || 20; @@ -85,13 +88,41 @@ const urlRegex = (() => { 'tel', ].join('|')}):`; return new RegExp( - `(?:${withAuth}|${withoutAuth}|www\\.)${urlChar}*(?!${postUrlPunct})${urlChar}`, 'g'); + `(?:${withAuth}|${withoutAuth}|www\\.)${urlChar}*(?!${postUrlPunct})${urlChar}`, 'g'); })(); // https://stackoverflow.com/a/68957976 const base64url = /^(?=(?:.{4})*$)[A-Za-z0-9_-]*(?:[AQgw]==|[AEIMQUYcgkosw048]=)?$/; -const padutils = { +type PadEvent = { + which: number +} + +type JQueryNode = JQuery + +class PadUtils { + public urlRegex: RegExp + public wordCharRegex: RegExp + public warnDeprecatedFlags: { + disabledForTestingOnly: boolean, + _rl?: { + prevs: Map, + now: () => number, + period: number + } + logger?: any + } + public globalExceptionHandler: null | any = null; + + + constructor() { + this.warnDeprecatedFlags = { + disabledForTestingOnly: false + } + this.wordCharRegex = wordCharRegex + this.urlRegex = urlRegex + } + /** * Prints a warning message followed by a stack trace (to make it easier to figure out what code * is using the deprecated function). @@ -107,41 +138,41 @@ const padutils = { * @param {...*} args - Passed to `padutils.warnDeprecated.logger.warn` (or `console.warn` if no * logger is set), with a stack trace appended if available. */ - warnDeprecated: (...args) => { - if (padutils.warnDeprecated.disabledForTestingOnly) return; + warnDeprecated = (...args: any[]) => { + if (this.warnDeprecatedFlags.disabledForTestingOnly) return; const err = new Error(); - if (Error.captureStackTrace) Error.captureStackTrace(err, padutils.warnDeprecated); + if (Error.captureStackTrace) Error.captureStackTrace(err, this.warnDeprecated); err.name = ''; // Rate limit identical deprecation warnings (as determined by the stack) to avoid log spam. if (typeof err.stack === 'string') { - if (padutils.warnDeprecated._rl == null) { - padutils.warnDeprecated._rl = - {prevs: new Map(), now: () => Date.now(), period: 10 * 60 * 1000}; + if (this.warnDeprecatedFlags._rl == null) { + this.warnDeprecatedFlags._rl = + {prevs: new Map(), now: () => Date.now(), period: 10 * 60 * 1000}; } - const rl = padutils.warnDeprecated._rl; + const rl = this.warnDeprecatedFlags._rl; const now = rl.now(); const prev = rl.prevs.get(err.stack); if (prev != null && now - prev < rl.period) return; rl.prevs.set(err.stack, now); } if (err.stack) args.push(err.stack); - (padutils.warnDeprecated.logger || console).warn(...args); - }, - - escapeHtml: (x) => Security.escapeHTML(String(x)), - uniqueId: () => { + (this.warnDeprecatedFlags.logger || console).warn(...args); + } + escapeHtml = (x: string) => Security.escapeHTML(String(x)) + uniqueId = () => { const pad = require('./pad').pad; // Sidestep circular dependency // returns string that is exactly 'width' chars, padding with zeros and taking rightmost digits const encodeNum = - (n, width) => (Array(width + 1).join('0') + Number(n).toString(35)).slice(-width); + (n: number, width: number) => (Array(width + 1).join('0') + Number(n).toString(35)).slice(-width); return [ pad.getClientIp(), encodeNum(+new Date(), 7), encodeNum(Math.floor(Math.random() * 1e9), 4), ].join('.'); - }, + } + // e.g. "Thu Jun 18 2009 13:09" - simpleDateTime: (date) => { + simpleDateTime = (date: string) => { const d = new Date(+date); // accept either number or date const dayOfWeek = (['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'])[d.getDay()]; const month = ([ @@ -162,16 +193,14 @@ const padutils = { const year = d.getFullYear(); const hourmin = `${d.getHours()}:${(`0${d.getMinutes()}`).slice(-2)}`; return `${dayOfWeek} ${month} ${dayOfMonth} ${year} ${hourmin}`; - }, - wordCharRegex, - urlRegex, + } // returns null if no URLs, or [[startIndex1, url1], [startIndex2, url2], ...] - findURLs: (text) => { + findURLs = (text: string) => { // Copy padutils.urlRegex so that the use of .exec() below (which mutates the RegExp object) // does not break other concurrent uses of padutils.urlRegex. - const urlRegex = new RegExp(padutils.urlRegex, 'g'); + const urlRegex = new RegExp(this.urlRegex, 'g'); urlRegex.lastIndex = 0; - let urls = null; + let urls: [number, string][] | null = null; let execResult; // TODO: Switch to String.prototype.matchAll() after support for Node.js < 12.0.0 is dropped. while ((execResult = urlRegex.exec(text))) { @@ -181,18 +210,19 @@ const padutils = { urls.push([startIndex, url]); } return urls; - }, - escapeHtmlWithClickableLinks: (text, target) => { + } + escapeHtmlWithClickableLinks = (text: string, target: string) => { let idx = 0; const pieces = []; - const urls = padutils.findURLs(text); + const urls = this.findURLs(text); - const advanceTo = (i) => { - if (i > idx) { - pieces.push(Security.escapeHTML(text.substring(idx, i))); - idx = i; + const advanceTo = (i: number) => { + if (i > idx) { + pieces.push(Security.escapeHTML(text.substring(idx, i))); + idx = i; + } } - }; + ; if (urls) { for (let j = 0; j < urls.length; j++) { const startIndex = urls[j][0]; @@ -206,25 +236,25 @@ const padutils = { // https://mathiasbynens.github.io/rel-noopener/ // https://github.com/ether/etherpad-lite/pull/3636 pieces.push( - ''); + ''); advanceTo(startIndex + href.length); pieces.push(''); } } advanceTo(text.length); return pieces.join(''); - }, - bindEnterAndEscape: (node, onEnter, onEscape) => { + } + bindEnterAndEscape = (node: JQueryNode, onEnter: Function, onEscape: Function) => { // Use keypress instead of keyup in bindEnterAndEscape. Keyup event is fired on enter in IME // (Input Method Editor), But keypress is not. So, I changed to use keypress instead of keyup. // It is work on Windows (IE8, Chrome 6.0.472), CentOs (Firefox 3.0) and Mac OSX (Firefox // 3.6.10, Chrome 6.0.472, Safari 5.0). if (onEnter) { - node.on('keypress', (evt) => { + node.on('keypress', (evt: { which: number; }) => { if (evt.which === 13) { onEnter(evt); } @@ -238,13 +268,15 @@ const padutils = { } }); } - }, - timediff: (d) => { + } + + timediff = (d: number) => { const pad = require('./pad').pad; // Sidestep circular dependency - const format = (n, word) => { - n = Math.round(n); - return (`${n} ${word}${n !== 1 ? 's' : ''} ago`); - }; + const format = (n: number, word: string) => { + n = Math.round(n); + return (`${n} ${word}${n !== 1 ? 's' : ''} ago`); + } + ; d = Math.max(0, (+(new Date()) - (+d) - pad.clientTimeOffset) / 1000); if (d < 60) { return format(d, 'second'); @@ -259,78 +291,89 @@ const padutils = { } d /= 24; return format(d, 'day'); - }, - makeAnimationScheduler: (funcToAnimateOneStep, stepTime, stepsAtOnce) => { - if (stepsAtOnce === undefined) { - stepsAtOnce = 1; + } + makeAnimationScheduler = + (funcToAnimateOneStep: any, stepTime: number, stepsAtOnce?: number) => { + if (stepsAtOnce === undefined) { + stepsAtOnce = 1; + } + + let animationTimer: any = null; + + const scheduleAnimation = () => { + if (!animationTimer) { + animationTimer = window.setTimeout(() => { + animationTimer = null; + let n = stepsAtOnce; + let moreToDo = true; + while (moreToDo && n > 0) { + moreToDo = funcToAnimateOneStep(); + n--; + } + if (moreToDo) { + // more to do + scheduleAnimation(); + } + }, stepTime * stepsAtOnce); + } + }; + return {scheduleAnimation}; } - let animationTimer = null; + makeFieldLabeledWhenEmpty + = + (field: JQueryNode, labelText: string) => { + field = $(field); - const scheduleAnimation = () => { - if (!animationTimer) { - animationTimer = window.setTimeout(() => { - animationTimer = null; - let n = stepsAtOnce; - let moreToDo = true; - while (moreToDo && n > 0) { - moreToDo = funcToAnimateOneStep(); - n--; - } - if (moreToDo) { - // more to do - scheduleAnimation(); - } - }, stepTime * stepsAtOnce); - } - }; - return {scheduleAnimation}; - }, - makeFieldLabeledWhenEmpty: (field, labelText) => { - field = $(field); - - const clear = () => { - field.addClass('editempty'); - field.val(labelText); - }; - field.focus(() => { - if (field.hasClass('editempty')) { - field.val(''); - } - field.removeClass('editempty'); - }); - field.on('blur', () => { - if (!field.val()) { - clear(); - } - }); - return { - clear, - }; - }, - getCheckbox: (node) => $(node).is(':checked'), - setCheckbox: (node, value) => { - if (value) { - $(node).attr('checked', 'checked'); - } else { - $(node).prop('checked', false); + const clear = () => { + field.addClass('editempty'); + field.val(labelText); + } + ; + field.focus(() => { + if (field.hasClass('editempty')) { + field.val(''); + } + field.removeClass('editempty'); + }); + field.on('blur', () => { + if (!field.val()) { + clear(); + } + }); + return { + clear, + }; } - }, - bindCheckboxChange: (node, func) => { - $(node).on('change', func); - }, - encodeUserId: (userId) => userId.replace(/[^a-y0-9]/g, (c) => { - if (c === '.') return '-'; - return `z${c.charCodeAt(0)}z`; - }), - decodeUserId: (encodedUserId) => encodedUserId.replace(/[a-y0-9]+|-|z.+?z/g, (cc) => { - if (cc === '-') { return '.'; } else if (cc.charAt(0) === 'z') { - return String.fromCharCode(Number(cc.slice(1, -1))); - } else { - return cc; + getCheckbox = (node: string) => $(node).is(':checked') + setCheckbox = + (node: JQueryNode, value: boolean) => { + if (value) { + $(node).attr('checked', 'checked'); + } else { + $(node).prop('checked', false); + } } - }), - + bindCheckboxChange = + (node: JQueryNode, func: Function) => { + // @ts-ignore + $(node).on("change", func); + } + encodeUserId = + (userId: string) => userId.replace(/[^a-y0-9]/g, (c) => { + if (c === '.') return '-'; + return `z${c.charCodeAt(0)}z`; + }) + decodeUserId = + (encodedUserId: string) => encodedUserId.replace(/[a-y0-9]+|-|z.+?z/g, (cc) => { + if (cc === '-') { + return '.'; + } else if (cc.charAt(0) === 'z') { + return String.fromCharCode(Number(cc.slice(1, -1))); + } else { + return cc; + } + }) /** * Returns whether a string has the expected format to be used as a secret token identifying an * author. The format is defined as: 't.' followed by a non-empty base64url string (RFC 4648 @@ -340,109 +383,109 @@ const padutils = { * conditional transformation of a token to a database key in a way that does not allow a * malicious user to impersonate another user). */ - isValidAuthorToken: (t) => { + isValidAuthorToken = (t: string | object) => { if (typeof t !== 'string' || !t.startsWith('t.')) return false; const v = t.slice(2); return v.length > 0 && base64url.test(v); - }, + } + /** * Returns a string that can be used in the `token` cookie as a secret that authenticates a * particular author. */ - generateAuthorToken: () => `t.${randomString()}`, -}; - -let globalExceptionHandler = null; -padutils.setupGlobalExceptionHandler = () => { - if (globalExceptionHandler == null) { - globalExceptionHandler = (e) => { - let type; - let err; - let msg, url, linenumber; - if (e instanceof ErrorEvent) { - type = 'Uncaught exception'; - err = e.error || {}; - ({message: msg, filename: url, lineno: linenumber} = e); - } else if (e instanceof PromiseRejectionEvent) { - type = 'Unhandled Promise rejection'; - err = e.reason || {}; - ({message: msg = 'unknown', fileName: url = 'unknown', lineNumber: linenumber = -1} = err); - } else { - throw new Error(`unknown event: ${e.toString()}`); - } - if (err.name != null && msg !== err.name && !msg.startsWith(`${err.name}: `)) { - msg = `${err.name}: ${msg}`; - } - const errorId = randomString(20); - - let msgAlreadyVisible = false; - $('.gritter-item .error-msg').each(function () { - if ($(this).text() === msg) { - msgAlreadyVisible = true; + generateAuthorToken = () => `t.${randomString()}` + setupGlobalExceptionHandler = () => { + if (this.globalExceptionHandler == null) { + this.globalExceptionHandler = (e: any) => { + let type; + let err; + let msg, url, linenumber; + if (e instanceof ErrorEvent) { + type = 'Uncaught exception'; + err = e.error || {}; + ({message: msg, filename: url, lineno: linenumber} = e); + } else if (e instanceof PromiseRejectionEvent) { + type = 'Unhandled Promise rejection'; + err = e.reason || {}; + ({message: msg = 'unknown', fileName: url = 'unknown', lineNumber: linenumber = -1} = err); + } else { + throw new Error(`unknown event: ${e.toString()}`); } - }); + if (err.name != null && msg !== err.name && !msg.startsWith(`${err.name}: `)) { + msg = `${err.name}: ${msg}`; + } + const errorId = randomString(20); - if (!msgAlreadyVisible) { - const txt = document.createTextNode.bind(document); // Convenience shorthand. - const errorMsg = [ - $('

') + let msgAlreadyVisible = false; + $('.gritter-item .error-msg').each(function () { + if ($(this).text() === msg) { + msgAlreadyVisible = true; + } + }); + + if (!msgAlreadyVisible) { + const txt = document.createTextNode.bind(document); // Convenience shorthand. + const errorMsg = [ + $('

') .append($('').text('Please press and hold Ctrl and press F5 to reload this page')), - $('

') + $('

') .text('If the problem persists, please send this error message to your webmaster:'), - $('

').css('text-align', 'left').css('font-size', '.8em').css('margin-top', '1em') + $('
').css('text-align', 'left').css('font-size', '.8em').css('margin-top', '1em') .append($('').addClass('error-msg').text(msg)).append($('
')) .append(txt(`at ${url} at line ${linenumber}`)).append($('
')) .append(txt(`ErrorId: ${errorId}`)).append($('
')) .append(txt(type)).append($('
')) .append(txt(`URL: ${window.location.href}`)).append($('
')) .append(txt(`UserAgent: ${navigator.userAgent}`)).append($('
')), - ]; + ]; - $.gritter.add({ - title: 'An error occurred', - text: errorMsg, - class_name: 'error', - position: 'bottom', - sticky: true, + // @ts-ignore + $.gritter.add({ + title: 'An error occurred', + text: errorMsg, + class_name: 'error', + position: 'bottom', + sticky: true, + }); + } + + // send javascript errors to the server + $.post('../jserror', { + errorInfo: JSON.stringify({ + errorId, + type, + msg, + url: window.location.href, + source: url, + linenumber, + userAgent: navigator.userAgent, + stack: err.stack, + }), }); - } - - // send javascript errors to the server - $.post('../jserror', { - errorInfo: JSON.stringify({ - errorId, - type, - msg, - url: window.location.href, - source: url, - linenumber, - userAgent: navigator.userAgent, - stack: err.stack, - }), - }); - }; - window.onerror = null; // Clear any pre-existing global error handler. - window.addEventListener('error', globalExceptionHandler); - window.addEventListener('unhandledrejection', globalExceptionHandler); + }; + window.onerror = null; // Clear any pre-existing global error handler. + window.addEventListener('error', this.globalExceptionHandler); + window.addEventListener('unhandledrejection', this.globalExceptionHandler); + } } -}; - -padutils.binarySearch = require('./ace2_common').binarySearch; + binarySearch = binarySearch +} // https://stackoverflow.com/a/42660748 const inThirdPartyIframe = () => { try { - return (!window.top.location.hostname); + return (!window.top!.location.hostname); } catch (e) { return true; } }; +export let Cookies: CookiesStatic // This file is included from Node so that it can reuse randomString, but Node doesn't have a global // window object. if (typeof window !== 'undefined') { - exports.Cookies = require('js-cookie').withAttributes({ + Cookies = jsCookie.withAttributes({ // Use `SameSite=Lax`, unless Etherpad is embedded in an iframe from another site in which case // use `SameSite=None`. For iframes from another site, only `None` has a chance of working // because the cookies are third-party (not same-site). Many browsers/users block third-party @@ -455,5 +498,5 @@ if (typeof window !== 'undefined') { secure: window.location.protocol === 'https:', }); } -exports.randomString = randomString; -exports.padutils = padutils; + +export default new PadUtils() diff --git a/src/static/js/pluginfw/client_plugins.js b/src/static/js/pluginfw/client_plugins.ts similarity index 99% rename from src/static/js/pluginfw/client_plugins.js rename to src/static/js/pluginfw/client_plugins.ts index 3a0687733..0688d12ca 100644 --- a/src/static/js/pluginfw/client_plugins.js +++ b/src/static/js/pluginfw/client_plugins.ts @@ -1,3 +1,4 @@ +// @ts-nocheck 'use strict'; const pluginUtils = require('./shared'); diff --git a/src/static/js/pluginfw/hooks.js b/src/static/js/pluginfw/hooks.ts similarity index 99% rename from src/static/js/pluginfw/hooks.js rename to src/static/js/pluginfw/hooks.ts index 731cbd190..a480ecf46 100644 --- a/src/static/js/pluginfw/hooks.js +++ b/src/static/js/pluginfw/hooks.ts @@ -1,3 +1,4 @@ +// @ts-nocheck 'use strict'; const pluginDefs = require('./plugin_defs'); diff --git a/src/static/js/pluginfw/plugin_defs.js b/src/static/js/pluginfw/plugin_defs.ts similarity index 100% rename from src/static/js/pluginfw/plugin_defs.js rename to src/static/js/pluginfw/plugin_defs.ts diff --git a/src/static/js/pluginfw/plugins.js b/src/static/js/pluginfw/plugins.ts similarity index 99% rename from src/static/js/pluginfw/plugins.js rename to src/static/js/pluginfw/plugins.ts index f48d6cc6d..97c1694e2 100644 --- a/src/static/js/pluginfw/plugins.js +++ b/src/static/js/pluginfw/plugins.ts @@ -1,3 +1,4 @@ +// @ts-nocheck 'use strict'; const fs = require('fs').promises; diff --git a/src/static/js/pluginfw/shared.js b/src/static/js/pluginfw/shared.ts similarity index 99% rename from src/static/js/pluginfw/shared.js rename to src/static/js/pluginfw/shared.ts index b2c2337f6..a7c761786 100644 --- a/src/static/js/pluginfw/shared.js +++ b/src/static/js/pluginfw/shared.ts @@ -1,3 +1,4 @@ +// @ts-nocheck 'use strict'; const defs = require('./plugin_defs'); diff --git a/src/static/js/pluginfw/tsort.js b/src/static/js/pluginfw/tsort.ts similarity index 99% rename from src/static/js/pluginfw/tsort.js rename to src/static/js/pluginfw/tsort.ts index 6f0b4bf00..a067d29de 100644 --- a/src/static/js/pluginfw/tsort.js +++ b/src/static/js/pluginfw/tsort.ts @@ -1,3 +1,4 @@ +// @ts-nocheck 'use strict'; /** diff --git a/src/static/js/rjquery.js b/src/static/js/rjquery.ts similarity index 93% rename from src/static/js/rjquery.js rename to src/static/js/rjquery.ts index a80e1f8d3..167e96090 100644 --- a/src/static/js/rjquery.js +++ b/src/static/js/rjquery.ts @@ -1,3 +1,4 @@ +// @ts-nocheck 'use strict'; // Provides a require'able version of jQuery without leaking $ and jQuery; window.$ = require('./vendors/jquery'); diff --git a/src/static/js/security.js b/src/static/js/security.ts similarity index 97% rename from src/static/js/security.js rename to src/static/js/security.ts index d92425cb7..d5f9b7266 100644 --- a/src/static/js/security.js +++ b/src/static/js/security.ts @@ -1,3 +1,4 @@ +// @ts-nocheck 'use strict'; /** diff --git a/src/static/js/skin_variants.js b/src/static/js/skin_variants.ts similarity index 99% rename from src/static/js/skin_variants.js rename to src/static/js/skin_variants.ts index 9a0427ac9..c9783aaa3 100644 --- a/src/static/js/skin_variants.js +++ b/src/static/js/skin_variants.ts @@ -1,3 +1,4 @@ +// @ts-nocheck 'use strict'; // Specific hash to display the skin variants builder popup diff --git a/src/static/js/skiplist.js b/src/static/js/skiplist.ts similarity index 61% rename from src/static/js/skiplist.js rename to src/static/js/skiplist.ts index f10a4e7a8..0e7724b23 100644 --- a/src/static/js/skiplist.js +++ b/src/static/js/skiplist.ts @@ -22,10 +22,24 @@ * limitations under the License. */ -const _entryWidth = (e) => (e && e.width) || 0; +const _entryWidth = (e: Entry) => (e && e.width) || 0; + +type Entry = { + key: string, + value?: string + width?: number +} class Node { - constructor(entry, levels = 0, downSkips = 1, downSkipWidths = 0) { + public key: string|null + readonly entry: Entry|null + levels: number + upPtrs: Node[] + downPtrs: Node[] + downSkips: number[] + readonly downSkipWidths: number[] + + constructor(entry: Entry|null, levels = 0, downSkips: number|null = 1, downSkipWidths:number|null = 0) { this.key = entry != null ? entry.key : null; this.entry = entry; this.levels = levels; @@ -37,9 +51,9 @@ class Node { propagateWidthChange() { const oldWidth = this.downSkipWidths[0]; - const newWidth = _entryWidth(this.entry); + const newWidth = _entryWidth(this.entry!); const widthChange = newWidth - oldWidth; - let n = this; + let n: Node = this; let lvl = 0; while (lvl < n.levels) { n.downSkipWidths[lvl] += widthChange; @@ -57,17 +71,23 @@ class Node { // is still valid and points to the same index in the skiplist. Other operations with other points // invalidate this point. class Point { - constructor(skipList, loc) { - this._skipList = skipList; + private skipList: SkipList + private readonly loc: number + private readonly idxs: number[] + private readonly nodes: Node[] + private widthSkips: number[] + + constructor(skipList: SkipList, loc: number) { + this.skipList = skipList; this.loc = loc; - const numLevels = this._skipList._start.levels; + const numLevels = this.skipList.start.levels; let lvl = numLevels - 1; let i = -1; let ws = 0; - const nodes = new Array(numLevels); - const idxs = new Array(numLevels); - const widthSkips = new Array(numLevels); - nodes[lvl] = this._skipList._start; + const nodes: Node[] = new Array(numLevels); + const idxs: number[] = new Array(numLevels); + const widthSkips: number[] = new Array(numLevels); + nodes[lvl] = this.skipList.start; idxs[lvl] = -1; widthSkips[lvl] = 0; while (lvl >= 0) { @@ -94,9 +114,9 @@ class Point { return `Point(${this.loc})`; } - insert(entry) { + insert(entry: Entry) { if (entry.key == null) throw new Error('entry.key must not be null'); - if (this._skipList.containsKey(entry.key)) { + if (this.skipList.containsKey(entry.key)) { throw new Error(`an entry with key ${entry.key} already exists`); } @@ -115,14 +135,14 @@ class Point { if (lvl === pNodes.length) { // assume we have just passed the end of this.nodes, and reached one level greater // than the skiplist currently supports - pNodes[lvl] = this._skipList._start; + pNodes[lvl] = this.skipList.start; pIdxs[lvl] = -1; - this._skipList._start.levels++; - this._skipList._end.levels++; - this._skipList._start.downPtrs[lvl] = this._skipList._end; - this._skipList._end.upPtrs[lvl] = this._skipList._start; - this._skipList._start.downSkips[lvl] = this._skipList._keyToNodeMap.size + 1; - this._skipList._start.downSkipWidths[lvl] = this._skipList._totalWidth; + this.skipList.start.levels++; + this.skipList.end.levels++; + this.skipList.start.downPtrs[lvl] = this.skipList.end; + this.skipList.end.upPtrs[lvl] = this.skipList.start; + this.skipList.start.downSkips[lvl] = this.skipList.keyToNodeMap.size + 1; + this.skipList.start.downSkipWidths[lvl] = this.skipList._totalWidth; this.widthSkips[lvl] = 0; } const me = newNode; @@ -146,13 +166,13 @@ class Point { up.downSkips[lvl]++; up.downSkipWidths[lvl] += newWidth; } - this._skipList._keyToNodeMap.set(newNode.key, newNode); - this._skipList._totalWidth += newWidth; + this.skipList.keyToNodeMap.set(newNode.key as string, newNode); + this.skipList._totalWidth += newWidth; } delete() { const elem = this.nodes[0].downPtrs[0]; - const elemWidth = _entryWidth(elem.entry); + const elemWidth = _entryWidth(elem.entry!); for (let i = 0; i < this.nodes.length; i++) { if (i < elem.levels) { const up = elem.upPtrs[i]; @@ -169,8 +189,8 @@ class Point { up.downSkipWidths[i] -= elemWidth; } } - this._skipList._keyToNodeMap.delete(elem.key); - this._skipList._totalWidth -= elemWidth; + this.skipList.keyToNodeMap.delete(elem.key as string); + this.skipList._totalWidth -= elemWidth; } getNode() { @@ -183,20 +203,26 @@ class Point { * property that is a string. */ class SkipList { + start: Node + end: Node + _totalWidth: number + keyToNodeMap: Map + + constructor() { // if there are N elements in the skiplist, "start" is element -1 and "end" is element N - this._start = new Node(null, 1); - this._end = new Node(null, 1, null, null); + this.start = new Node(null, 1); + this.end = new Node(null, 1, null, null); this._totalWidth = 0; - this._keyToNodeMap = new Map(); - this._start.downPtrs[0] = this._end; - this._end.upPtrs[0] = this._start; + this.keyToNodeMap = new Map(); + this.start.downPtrs[0] = this.end; + this.end.upPtrs[0] = this.start; } - _getNodeAtOffset(targetOffset) { + _getNodeAtOffset(targetOffset: number) { let i = 0; - let n = this._start; - let lvl = this._start.levels - 1; + let n = this.start; + let lvl = this.start.levels - 1; while (lvl >= 0 && n.downPtrs[lvl]) { while (n.downPtrs[lvl] && (i + n.downSkipWidths[lvl] <= targetOffset)) { i += n.downSkipWidths[lvl]; @@ -204,17 +230,17 @@ class SkipList { } lvl--; } - if (n === this._start) return (this._start.downPtrs[0] || null); - if (n === this._end) { - return targetOffset === this._totalWidth ? (this._end.upPtrs[0] || null) : null; + if (n === this.start) return (this.start.downPtrs[0] || null); + if (n === this.end) { + return targetOffset === this._totalWidth ? (this.end.upPtrs[0] || null) : null; } return n; } - _getNodeIndex(node, byWidth) { + _getNodeIndex(node: Node, byWidth?: boolean) { let dist = (byWidth ? 0 : -1); let n = node; - while (n !== this._start) { + while (n !== this.start) { const lvl = n.levels - 1; n = n.upPtrs[lvl]; if (byWidth) dist += n.downSkipWidths[lvl]; @@ -223,17 +249,19 @@ class SkipList { return dist; } + totalWidth() { return this._totalWidth; } + // Returns index of first entry such that entryFunc(entry) is truthy, // or length() if no such entry. Assumes all falsy entries come before // all truthy entries. - search(entryFunc) { - let low = this._start; - let lvl = this._start.levels - 1; + search(entryFunc: Function) { + let low = this.start; + let lvl = this.start.levels - 1; let lowIndex = -1; - const f = (node) => { - if (node === this._start) return false; - else if (node === this._end) return true; + const f = (node: Node) => { + if (node === this.start) return false; + else if (node === this.end) return true; else return entryFunc(node.entry); }; @@ -249,20 +277,20 @@ class SkipList { return lowIndex + 1; } - length() { return this._keyToNodeMap.size; } + length() { return this.keyToNodeMap.size; } - atIndex(i) { + atIndex(i: number) { if (i < 0) console.warn(`atIndex(${i})`); - if (i >= this._keyToNodeMap.size) console.warn(`atIndex(${i}>=${this._keyToNodeMap.size})`); + if (i >= this.keyToNodeMap.size) console.warn(`atIndex(${i}>=${this.keyToNodeMap.size})`); return (new Point(this, i)).getNode().entry; } // differs from Array.splice() in that new elements are in an array, not varargs - splice(start, deleteCount, newEntryArray) { + splice(start: number, deleteCount: number, newEntryArray: Entry[]) { if (start < 0) console.warn(`splice(${start}, ...)`); - if (start + deleteCount > this._keyToNodeMap.size) { - console.warn(`splice(${start}, ${deleteCount}, ...), N=${this._keyToNodeMap.size}`); - console.warn('%s %s %s', typeof start, typeof deleteCount, typeof this._keyToNodeMap.size); + if (start + deleteCount > this.keyToNodeMap.size) { + console.warn(`splice(${start}, ${deleteCount}, ...), N=${this.keyToNodeMap.size}`); + console.warn('%s %s %s', typeof start, typeof deleteCount, typeof this.keyToNodeMap.size); console.trace(); } @@ -275,56 +303,55 @@ class SkipList { } } - next(entry) { return this._keyToNodeMap.get(entry.key).downPtrs[0].entry || null; } - prev(entry) { return this._keyToNodeMap.get(entry.key).upPtrs[0].entry || null; } - push(entry) { this.splice(this._keyToNodeMap.size, 0, [entry]); } + next(entry: Entry) { return this.keyToNodeMap.get(entry.key)!.downPtrs[0].entry || null; } + prev(entry: Entry) { return this.keyToNodeMap.get(entry.key)!.upPtrs[0].entry || null; } + push(entry: Entry) { this.splice(this.keyToNodeMap.size, 0, [entry]); } - slice(start, end) { + slice(start: number, end: number) { // act like Array.slice() if (start === undefined) start = 0; - else if (start < 0) start += this._keyToNodeMap.size; - if (end === undefined) end = this._keyToNodeMap.size; - else if (end < 0) end += this._keyToNodeMap.size; + else if (start < 0) start += this.keyToNodeMap.size; + if (end === undefined) end = this.keyToNodeMap.size; + else if (end < 0) end += this.keyToNodeMap.size; if (start < 0) start = 0; - if (start > this._keyToNodeMap.size) start = this._keyToNodeMap.size; + if (start > this.keyToNodeMap.size) start = this.keyToNodeMap.size; if (end < 0) end = 0; - if (end > this._keyToNodeMap.size) end = this._keyToNodeMap.size; + if (end > this.keyToNodeMap.size) end = this.keyToNodeMap.size; if (end <= start) return []; let n = this.atIndex(start); const array = [n]; for (let i = 1; i < (end - start); i++) { - n = this.next(n); + n = this.next(n!); array.push(n); } return array; } - atKey(key) { return this._keyToNodeMap.get(key).entry; } - indexOfKey(key) { return this._getNodeIndex(this._keyToNodeMap.get(key)); } - indexOfEntry(entry) { return this.indexOfKey(entry.key); } - containsKey(key) { return this._keyToNodeMap.has(key); } + atKey(key: string) { return this.keyToNodeMap.get(key)!.entry; } + indexOfKey(key: string) { return this._getNodeIndex(this.keyToNodeMap.get(key)!); } + indexOfEntry(entry: Entry) { return this.indexOfKey(entry.key); } + containsKey(key: string) { return this.keyToNodeMap.has(key); } // gets the last entry starting at or before the offset - atOffset(offset) { return this._getNodeAtOffset(offset).entry; } - keyAtOffset(offset) { return this.atOffset(offset).key; } - offsetOfKey(key) { return this._getNodeIndex(this._keyToNodeMap.get(key), true); } - offsetOfEntry(entry) { return this.offsetOfKey(entry.key); } - setEntryWidth(entry, width) { + atOffset(offset: number) { return this._getNodeAtOffset(offset)!.entry; } + keyAtOffset(offset: number) { return this.atOffset(offset)!.key; } + offsetOfKey(key: string) { return this._getNodeIndex(this.keyToNodeMap.get(key)!, true); } + offsetOfEntry(entry: Entry) { return this.offsetOfKey(entry.key); } + setEntryWidth(entry: Entry, width: number) { entry.width = width; - this._totalWidth += this._keyToNodeMap.get(entry.key).propagateWidthChange(); + this._totalWidth += this.keyToNodeMap.get(entry.key)!.propagateWidthChange(); } - totalWidth() { return this._totalWidth; } - offsetOfIndex(i) { + offsetOfIndex(i: number) { if (i < 0) return 0; - if (i >= this._keyToNodeMap.size) return this._totalWidth; - return this.offsetOfEntry(this.atIndex(i)); + if (i >= this.keyToNodeMap.size) return this._totalWidth; + return this.offsetOfEntry(this.atIndex(i)!); } - indexOfOffset(offset) { + indexOfOffset(offset: number) { if (offset <= 0) return 0; - if (offset >= this._totalWidth) return this._keyToNodeMap.size; - return this.indexOfEntry(this.atOffset(offset)); + if (offset >= this._totalWidth) return this.keyToNodeMap.size; + return this.indexOfEntry(this.atOffset(offset)!); } } -module.exports = SkipList; +export default SkipList diff --git a/src/static/js/socketio.js b/src/static/js/socketio.ts similarity index 99% rename from src/static/js/socketio.js rename to src/static/js/socketio.ts index cdc1c9a23..aedd68c20 100644 --- a/src/static/js/socketio.js +++ b/src/static/js/socketio.ts @@ -1,3 +1,4 @@ +// @ts-nocheck import io from 'socket.io-client'; /** diff --git a/src/static/js/timeslider.js b/src/static/js/timeslider.ts similarity index 97% rename from src/static/js/timeslider.js rename to src/static/js/timeslider.ts index 8d8604b91..befc4f8b7 100644 --- a/src/static/js/timeslider.js +++ b/src/static/js/timeslider.ts @@ -1,3 +1,4 @@ +// @ts-nocheck 'use strict'; /** @@ -26,10 +27,9 @@ // assigns to the global `$` and augments it with plugins. require('./vendors/jquery'); -const Cookies = require('./pad_utils').Cookies; -const randomString = require('./pad_utils').randomString; +import {randomString, Cookies} from "./pad_utils"; const hooks = require('./pluginfw/hooks'); -const padutils = require('./pad_utils').padutils; +import padutils from './pad_utils' const socketio = require('./socketio'); import html10n from '../js/vendors/html10n' let token, padId, exportLinks, socket, changesetLoader, BroadcastSlider; diff --git a/src/static/js/types/AText.ts b/src/static/js/types/AText.ts new file mode 100644 index 000000000..d58626de2 --- /dev/null +++ b/src/static/js/types/AText.ts @@ -0,0 +1,4 @@ +export type AText = { + text: string, + attribs: string, +} diff --git a/src/static/js/types/Attribute.ts b/src/static/js/types/Attribute.ts new file mode 100644 index 000000000..f1c06b3cb --- /dev/null +++ b/src/static/js/types/Attribute.ts @@ -0,0 +1 @@ +export type Attribute = [string, string] diff --git a/src/static/js/types/PadRevision.ts b/src/static/js/types/PadRevision.ts new file mode 100644 index 000000000..c54dc4379 --- /dev/null +++ b/src/static/js/types/PadRevision.ts @@ -0,0 +1,7 @@ +export type PadRevision = { + revNum: number; + savedById: string; + label: string; + timestamp: number; + id: string; +} diff --git a/src/static/js/types/SocketIOMessage.ts b/src/static/js/types/SocketIOMessage.ts new file mode 100644 index 000000000..9c9ffea7f --- /dev/null +++ b/src/static/js/types/SocketIOMessage.ts @@ -0,0 +1,317 @@ +import {MapArrayType} from "../../../node/types/MapType"; +import {AText} from "./AText"; +import AttributePool from "../AttributePool"; +import attributePool from "../AttributePool"; +import ChatMessage from "../ChatMessage"; +import {PadRevision} from "./PadRevision"; + +export type Part = { + name: string, + client_hooks: MapArrayType, + hooks: MapArrayType + pre?: string[] + post?: string[] + plugin?: string +} + + +export type MappedPlugin = Part& { + plugin: string + full_name: string +} + +export type SocketIOMessage = { + type: string + accessStatus: string +} + +export type HistoricalAuthorData = MapArrayType<{ + name: string; + colorId: number; + userId?: string +}> + +export type ServerVar = { + rev: number + clientIp: string + padId: string + historicalAuthorData?: HistoricalAuthorData, + initialAttributedText: { + attribs: string + text: string + }, + apool: AttributePoolWire + time: number +} + +export type AttributePoolWire = {numToAttrib: {[p: number]: [string, string]}, nextNum: number} + + +export type UserInfo = { + userId: string + colorId: string, + name: string|null +} + +export type ClientVarPayload = { + readOnlyId: string + automaticReconnectionTimeout: number + sessionRefreshInterval: number, + atext?: AText, + apool?: AttributePool, + userName?: string, + userColor: number, + hideChat?: boolean, + padOptions: PadOption, + padId: string, + clientIp: string, + colorPalette: string[], + accountPrivs: { + maxRevisions: number, + }, + collab_client_vars: ServerVar, + chatHead: number, + readonly: boolean, + serverTimestamp: number, + initialOptions: MapArrayType, + userId: string, + mode: string, + randomVersionString: string, + skinName: string + skinVariants: string, + exportAvailable: string + savedRevisions: PadRevision[], + initialRevisionList: number[], + padShortcutEnabled: MapArrayType, + initialTitle: string, + opts: {} + numConnectedUsers: number + abiwordAvailable: string + sofficeAvailable: string + plugins: { + plugins: MapArrayType + parts: MappedPlugin[] + } + indentationOnNewLine: boolean + scrollWhenFocusLineIsOutOfViewport : { + percentage: { + editionAboveViewport: number, + editionBelowViewport: number + } + duration: number + scrollWhenCaretIsInTheLastLineOfViewport: boolean + percentageToScrollWhenUserPressesArrowUp: number + } + initialChangesets: [] +} + +export type ClientVarData = { + type: "CLIENT_VARS" + data: ClientVarPayload +} + +export type ClientNewChanges = { + type : 'NEW_CHANGES' + apool: AttributePool, + author: string, + changeset: string, + newRev: number, + payload?: ClientNewChanges +} + +export type ClientAcceptCommitMessage = { + type: 'ACCEPT_COMMIT' + newRev: number +} + +export type ClientConnectMessage = { + type: 'CLIENT_RECONNECT', + noChanges: boolean, + headRev: number, + newRev: number, + changeset: string, + author: string + apool: AttributePool +} + + +export type UserNewInfoMessage = { + type: 'USER_NEWINFO', + data: { + userInfo: UserInfo + } +} + +export type UserLeaveMessage = { + type: 'USER_LEAVE' + userInfo: UserInfo +} + + + +export type ClientMessageMessage = { + type: 'CLIENT_MESSAGE', + payload: ClientSendMessages +} + +export type ChatMessageMessage = { + type: 'CHAT_MESSAGE' + data: { + message: ChatMessage + } +} + +export type ChatMessageMessages = { + type: 'CHAT_MESSAGES' + messages: string +} + +export type ClientUserChangesMessage = { + type: 'USER_CHANGES', + baseRev: number, + changeset: string, + apool: attributePool +} + + + +export type ClientSendMessages = ClientUserChangesMessage |ClientReadyMessage| ClientSendUserInfoUpdate|ChatMessageMessage| ClientMessageMessage | GetChatMessageMessage |ClientSuggestUserName | NewRevisionListMessage | RevisionLabel | PadOptionsMessage| ClientSaveRevisionMessage + +export type ClientReadyMessage = { + type: 'CLIENT_READY', + component: string, + padId: string, + sessionID: string, + token: string, + userInfo: UserInfo, + reconnect?: boolean + client_rev?: number +} + +export type ClientSaveRevisionMessage = { + type: 'SAVE_REVISION' +} + +export type GetChatMessageMessage = { + type: 'GET_CHAT_MESSAGES', + start: number, + end: number +} + +export type ClientSendUserInfoUpdate = { + type: 'USERINFO_UPDATE', + userInfo: UserInfo +} + +export type ClientSuggestUserName = { + type: 'suggestUserName', + data: { + payload: { + unnamedId: string, + newName: string + } + } +} + +export type NewRevisionListMessage = { + type: 'newRevisionList', + revisionList: number[] +} + +export type RevisionLabel = { + type: 'revisionLabel' + revisionList: number[] +} + +export type PadOptionsMessage = { + type: 'padoptions' + options: PadOption + changedBy: string +} + +export type PadOption = { + "noColors"?: boolean, + "showControls"?: boolean, + "showChat"?: boolean, + "showLineNumbers"?: boolean, + "useMonospaceFont"?: boolean, + "userName"?: null|string, + "userColor"?: null|string, + "rtl"?: boolean, + "alwaysShowChat"?: boolean, + "chatAndUsers"?: boolean, + "lang"?: null|string, + view? : MapArrayType +} + + +type SharedMessageType = { + payload:{ + timestamp: number + } +} + +export type x = { + disconnect: boolean +} + +export type ClientDisconnectedMessage = { + type: "disconnected" + disconnected: boolean +} + +export type UserChanges = { + data: ClientUserChangesMessage +} + +export type UserSuggestUserName = { + data: { + payload: ClientSuggestUserName + } +} + +export type ChangesetRequestMessage = { + type: 'CHANGESET_REQ' + data: { + granularity: number + start: number + requestID: string + } +} + + + +export type CollabroomMessage = { + type: 'COLLABROOM' + data: ClientSendUserInfoUpdate | ClientUserChangesMessage | ChatMessageMessage | GetChatMessageMessage | ClientSaveRevisionMessage | ClientMessageMessage +} + +export type ClientVarMessage = | ClientVarData | ClientDisconnectedMessage | ClientReadyMessage| ChangesetRequestMessage | CollabroomMessage | CustomMessage + + +export type CustomMessage = { + type: 'CUSTOM' + data: any +} + +export type ClientCustomMessage = { + type: 'CUSTOM', + action: string, + payload: any + +} + +export type SocketClientReadyMessage = { + type: string + component: string + padId: string + sessionID: string + token: string + userInfo: { + colorId: string|null + name: string|null + }, + reconnect?: boolean + client_rev?: number +} + diff --git a/src/static/js/underscore.js b/src/static/js/underscore.ts similarity index 78% rename from src/static/js/underscore.js rename to src/static/js/underscore.ts index d30543cab..79a3e8e7f 100644 --- a/src/static/js/underscore.js +++ b/src/static/js/underscore.ts @@ -1,3 +1,4 @@ +// @ts-nocheck 'use strict'; module.exports = require('underscore'); diff --git a/src/static/js/undomodule.js b/src/static/js/undomodule.ts similarity index 99% rename from src/static/js/undomodule.js rename to src/static/js/undomodule.ts index d0b83419d..32f651248 100644 --- a/src/static/js/undomodule.js +++ b/src/static/js/undomodule.ts @@ -1,3 +1,4 @@ +// @ts-nocheck 'use strict'; /** diff --git a/src/static/js/vendors/browser.js b/src/static/js/vendors/browser.ts similarity index 99% rename from src/static/js/vendors/browser.js rename to src/static/js/vendors/browser.ts index 2516a325b..a785d8a8e 100644 --- a/src/static/js/vendors/browser.js +++ b/src/static/js/vendors/browser.ts @@ -1,3 +1,4 @@ +// @ts-nocheck // WARNING: This file may have been modified from original. // TODO: Check requirement of this file, this afaik was to cover weird edge cases // that have probably been fixed in browsers. diff --git a/src/static/js/vendors/farbtastic.js b/src/static/js/vendors/farbtastic.ts similarity index 99% rename from src/static/js/vendors/farbtastic.js rename to src/static/js/vendors/farbtastic.ts index 5d0c4718c..d9a4d6311 100644 --- a/src/static/js/vendors/farbtastic.js +++ b/src/static/js/vendors/farbtastic.ts @@ -1,3 +1,4 @@ +// @ts-nocheck // WARNING: This file has been modified from original. // TODO: Replace with https://github.com/Simonwep/pickr diff --git a/src/static/js/vendors/gritter.js b/src/static/js/vendors/gritter.ts similarity index 99% rename from src/static/js/vendors/gritter.js rename to src/static/js/vendors/gritter.ts index 1b8b9a759..993514c74 100644 --- a/src/static/js/vendors/gritter.js +++ b/src/static/js/vendors/gritter.ts @@ -1,3 +1,4 @@ +// @ts-nocheck // WARNING: This file has been modified from the Original /* diff --git a/src/static/js/vendors/jquery.js b/src/static/js/vendors/jquery.ts similarity index 99% rename from src/static/js/vendors/jquery.js rename to src/static/js/vendors/jquery.ts index bca73371f..1b9923a62 100644 --- a/src/static/js/vendors/jquery.js +++ b/src/static/js/vendors/jquery.ts @@ -1,3 +1,4 @@ +// @ts-nocheck /*! * jQuery JavaScript Library v3.7.1 * https://jquery.com/ diff --git a/src/static/js/vendors/nice-select.js b/src/static/js/vendors/nice-select.ts similarity index 99% rename from src/static/js/vendors/nice-select.js rename to src/static/js/vendors/nice-select.ts index 447ff6413..ec76b3cef 100644 --- a/src/static/js/vendors/nice-select.js +++ b/src/static/js/vendors/nice-select.ts @@ -1,3 +1,4 @@ +// @ts-nocheck // WARNING: This file has been modified from the Original // TODO: Nice Select seems relatively abandoned, we should consider other options. diff --git a/src/templates/timeslider.html b/src/templates/timeslider.html index e2178e54e..4ef6928ff 100644 --- a/src/templates/timeslider.html +++ b/src/templates/timeslider.html @@ -29,7 +29,6 @@ for the JavaScript code in this page.| */ - diff --git a/src/tests/backend-new/specs/AttributeMap.ts b/src/tests/backend-new/specs/AttributeMap.ts index 3e43abf7c..ce5e61f74 100644 --- a/src/tests/backend-new/specs/AttributeMap.ts +++ b/src/tests/backend-new/specs/AttributeMap.ts @@ -1,17 +1,18 @@ 'use strict'; -const AttributeMap = require('../../../static/js/AttributeMap.js'); -const AttributePool = require('../../../static/js/AttributePool'); -const attributes = require('../../../static/js/attributes'); +import AttributeMap from '../../../static/js/AttributeMap'; +import AttributePool from '../../../static/js/AttributePool'; +import attributes from '../../../static/js/attributes'; import {expect, describe, it, beforeEach} from 'vitest' +import {Attribute} from "../../../static/js/types/Attribute"; describe('AttributeMap', function () { - const attribs = [ + const attribs: Attribute[] = [ ['foo', 'bar'], ['baz', 'bif'], ['emptyValue', ''], ]; - let pool: { eachAttrib: (arg0: () => number) => void; putAttrib: (arg0: string[]) => any; getAttrib: (arg0: number) => any; }; + let pool: AttributePool; const getPoolSize = () => { let n = 0; @@ -70,12 +71,14 @@ describe('AttributeMap', function () { describe(desc as string, function () { it('key is coerced to string', async function () { const m = new AttributeMap(pool); + // @ts-ignore m.set(input, 'value'); expect(m.get(want)).to.equal('value'); }); it('value is coerced to string', async function () { const m = new AttributeMap(pool); + // @ts-ignore m.set('key', input); expect(m.get('key')).to.equal(want); }); @@ -122,6 +125,7 @@ describe('AttributeMap', function () { // @ts-ignore args[0] = attributes.attribsToString(attributes.sort([...args[0]]), pool); } + // @ts-ignore return AttributeMap.prototype[funcName].call(m, ...args); }; diff --git a/src/tests/backend-new/specs/attributes.ts b/src/tests/backend-new/specs/attributes.ts index 5f33f52b3..64a4464bd 100644 --- a/src/tests/backend-new/specs/attributes.ts +++ b/src/tests/backend-new/specs/attributes.ts @@ -2,14 +2,15 @@ import {APool} from "../../../node/types/PadType"; -const AttributePool = require('../../../static/js/AttributePool'); -const attributes = require('../../../static/js/attributes'); +import AttributePool from '../../../static/js/AttributePool'; +import attributes from '../../../static/js/attributes'; import {expect, describe, it, beforeEach} from 'vitest'; +import {Attribute} from "../../../static/js/types/Attribute"; describe('attributes', function () { - const attribs = [['foo', 'bar'], ['baz', 'bif']]; - let pool: APool; + const attribs: Attribute[] = [['foo', 'bar'], ['baz', 'bif']]; + let pool: AttributePool; beforeEach(async function () { pool = new AttributePool(); @@ -45,6 +46,7 @@ describe('attributes', function () { ]; for (const [input, want] of testCases) { it(`${JSON.stringify(input)} -> ${JSON.stringify(want)}`, async function () { + // @ts-ignore const got = [...attributes.decodeAttribString(input)]; expect(JSON.stringify(got)).to.equal(JSON.stringify(want)); }); @@ -61,6 +63,7 @@ describe('attributes', function () { ]; for (const [desc, input] of testCases) { it(desc as string, async function () { + // @ts-ignore expect(attributes.encodeAttribString(input)).to.equal('*0*1'); }); } @@ -78,6 +81,7 @@ describe('attributes', function () { ]; for (const [input, wantErr] of testCases) { it(JSON.stringify(input), async function () { + // @ts-ignore expect(() => attributes.encodeAttribString(input)).toThrowError(wantErr as RegExp); }); } @@ -97,6 +101,7 @@ describe('attributes', function () { ]; for (const [input, want] of testCases) { it(`${JSON.stringify(input)} -> ${JSON.stringify(want)}`, async function () { + // @ts-ignore expect(attributes.encodeAttribString(input)).to.equal(want); }); } @@ -117,6 +122,7 @@ describe('attributes', function () { for (const [desc, input] of testCases) { it(desc as string, async function () { + // @ts-ignore const gotAttribs = [...attributes.attribsFromNums(input, pool)]; expect(JSON.stringify(gotAttribs)).to.equal(JSON.stringify(attribs)); }); @@ -136,6 +142,7 @@ describe('attributes', function () { ]; for (const [input, wantErr] of testCases) { it(JSON.stringify(input), async function () { + // @ts-ignore expect(() => [...attributes.attribsFromNums(input, pool)]).toThrowError(wantErr as RegExp); }); } @@ -151,6 +158,7 @@ describe('attributes', function () { ]; for (const [input, want] of testCases) { it(`${JSON.stringify(input)} -> ${JSON.stringify(want)}`, async function () { + // @ts-ignore const gotAttribs = [...attributes.attribsFromNums(input, pool)]; expect(JSON.stringify(gotAttribs)).to.equal(JSON.stringify(want)); }); @@ -172,6 +180,7 @@ describe('attributes', function () { for (const [desc, input] of testCases) { it(desc as string, async function () { + // @ts-ignore const gotNums = [...attributes.attribsToNums(input, pool)]; expect(JSON.stringify(gotNums)).to.equal(JSON.stringify([0, 1])); }); @@ -182,6 +191,7 @@ describe('attributes', function () { const testCases = [null, [null]]; for (const input of testCases) { it(JSON.stringify(input), async function () { + // @ts-ignore expect(() => [...attributes.attribsToNums(input, pool)]).toThrowError(); }); } @@ -197,6 +207,7 @@ describe('attributes', function () { ]; for (const [input, want] of testCases) { it(`${JSON.stringify(input)} -> ${JSON.stringify(want)}`, async function () { + // @ts-ignore const got = [...attributes.attribsToNums(input, pool)]; expect(JSON.stringify(got)).to.equal(JSON.stringify(want)); }); @@ -211,6 +222,7 @@ describe('attributes', function () { ]; for (const [input, want] of testCases) { it(`${JSON.stringify(input)} -> ${JSON.stringify(want)}`, async function () { + // @ts-ignore const got = [...attributes.attribsToNums(input, pool)]; expect(JSON.stringify(got)).to.equal(JSON.stringify(want)); expect(JSON.stringify(pool.getAttrib(attribs.length))) @@ -234,6 +246,7 @@ describe('attributes', function () { ['value is coerced to string', [['key', inputVal]], [['key', wantVal]]], ]) { it(desc as string, async function () { + // @ts-ignore const gotNums = [...attributes.attribsToNums(inputAttribs, pool)]; // Each attrib in inputAttribs is expected to be new to the pool. const wantNums = [...Array(attribs.length + 1).keys()].slice(attribs.length); @@ -265,6 +278,7 @@ describe('attributes', function () { ]; for (const [input, wantErr] of testCases) { it(JSON.stringify(input), async function () { + // @ts-ignore expect(() => [...attributes.attribsFromString(input, pool)]).toThrowError(wantErr); }); } @@ -280,6 +294,7 @@ describe('attributes', function () { ]; for (const [input, want] of testCases) { it(`${JSON.stringify(input)} -> ${JSON.stringify(want)}`, async function () { + // @ts-ignore const gotAttribs = [...attributes.attribsFromString(input, pool)]; expect(JSON.stringify(gotAttribs)).to.equal(JSON.stringify(want)); }); @@ -297,6 +312,7 @@ describe('attributes', function () { for (const [desc, input] of testCases) { it(desc as string, async function () { + // @ts-ignore const got = attributes.attribsToString(input, pool); expect(got).to.equal('*0*1'); }); @@ -307,6 +323,7 @@ describe('attributes', function () { const testCases = [null, [null]]; for (const input of testCases) { it(JSON.stringify(input), async function () { + // @ts-ignore expect(() => attributes.attribsToString(input, pool)).toThrowError(); }); } @@ -322,6 +339,7 @@ describe('attributes', function () { ]; for (const [input, want] of testCases) { it(`${JSON.stringify(input)} -> ${JSON.stringify(want)}`, async function () { + // @ts-ignore const got = attributes.attribsToString(input, pool); expect(got).to.equal(want); }); @@ -336,6 +354,7 @@ describe('attributes', function () { ]; for (const [input, want] of testCases) { it(`${JSON.stringify(input)} -> ${JSON.stringify(want)}`, async function () { + // @ts-ignore const got = attributes.attribsToString(input, pool); expect(got).to.equal(want); expect(JSON.stringify(pool.getAttrib(attribs.length))) diff --git a/src/tests/backend-new/specs/contentcollector.ts b/src/tests/backend-new/specs/contentcollector.ts deleted file mode 100644 index 81f0bc2ea..000000000 --- a/src/tests/backend-new/specs/contentcollector.ts +++ /dev/null @@ -1,398 +0,0 @@ -'use strict'; - -/* - * While importexport tests target the `setHTML` API endpoint, which is nearly identical to what - * happens when a user manually imports a document via the UI, the contentcollector tests here don't - * use rehype to process the document. Rehype removes spaces and newĺines were applicable, so the - * expected results here can differ from importexport.js. - * - * If you add tests here, please also add them to importexport.js - */ - -import {APool} from "../../../node/types/PadType"; - -const AttributePool = require('../../../static/js/AttributePool'); -const Changeset = require('../../../static/js/Changeset'); -const assert = require('assert').strict; -const attributes = require('../../../static/js/attributes'); -const contentcollector = require('../../../static/js/contentcollector'); -const jsdom = require('jsdom'); - -import {describe, it, beforeAll, test} from 'vitest'; - -// All test case `wantAlines` values must only refer to attributes in this list so that the -// attribute numbers do not change due to changes in pool insertion order. -const knownAttribs = [ - ['insertorder', 'first'], - ['italic', 'true'], - ['list', 'bullet1'], - ['list', 'bullet2'], - ['list', 'number1'], - ['list', 'number2'], - ['lmkr', '1'], - ['start', '1'], - ['start', '2'], -]; - -const testCases = [ - { - description: 'Simple', - html: '

foo

', - wantAlines: ['+3'], - wantText: ['foo'], - }, - { - description: 'Line starts with asterisk', - html: '

*foo

', - wantAlines: ['+4'], - wantText: ['*foo'], - }, - { - description: 'Complex nested Li', - html: '
  1. one
    1. 1.1
  2. two
', - wantAlines: [ - '*0*4*6*7+1+3', - '*0*5*6*8+1+3', - '*0*4*6*8+1+3', - ], - wantText: [ - '*one', '*1.1', '*two', - ], - }, - { - description: 'Complex list of different types', - html: '
  • one
  • two
  • 0
  • 1
  • 2
    • 3
    • 4
  1. item
    1. item1
    2. item2
', - wantAlines: [ - '*0*2*6+1+3', - '*0*2*6+1+3', - '*0*2*6+1+1', - '*0*2*6+1+1', - '*0*2*6+1+1', - '*0*3*6+1+1', - '*0*3*6+1+1', - '*0*4*6*7+1+4', - '*0*5*6*8+1+5', - '*0*5*6*8+1+5', - ], - wantText: [ - '*one', - '*two', - '*0', - '*1', - '*2', - '*3', - '*4', - '*item', - '*item1', - '*item2', - ], - }, - { - description: 'Tests if uls properly get attributes', - html: '
  • a
  • b
div

foo

', - wantAlines: [ - '*0*2*6+1+1', - '*0*2*6+1+1', - '+3', - '+3', - ], - wantText: ['*a', '*b', 'div', 'foo'], - }, - { - description: 'Tests if indented uls properly get attributes', - html: '
  • a
    • b
  • a

foo

', - wantAlines: [ - '*0*2*6+1+1', - '*0*3*6+1+1', - '*0*2*6+1+1', - '+3', - ], - wantText: ['*a', '*b', '*a', 'foo'], - }, - { - description: 'Tests if ols properly get line numbers when in a normal OL', - html: '
  1. a
  2. b
  3. c

test

', - wantAlines: [ - '*0*4*6*7+1+1', - '*0*4*6*7+1+1', - '*0*4*6*7+1+1', - '+4', - ], - wantText: ['*a', '*b', '*c', 'test'], - noteToSelf: 'Ensure empty P does not induce line attribute marker, wont this break the editor?', - }, - { - description: 'A single completely empty line break within an ol should reset count if OL is closed off..', - html: '
  1. should be 1

hello

  1. should be 1
  2. should be 2

', - wantAlines: [ - '*0*4*6*7+1+b', - '+5', - '*0*4*6*8+1+b', - '*0*4*6*8+1+b', - '', - ], - wantText: ['*should be 1', 'hello', '*should be 1', '*should be 2', ''], - noteToSelf: "Shouldn't include attribute marker in the

line", - }, - { - description: 'A single

should create a new line', - html: '

', - wantAlines: ['', ''], - wantText: ['', ''], - noteToSelf: '

should create a line break but not break numbering', - }, - { - description: 'Tests if ols properly get line numbers when in a normal OL #2', - html: 'a
  1. b
    1. c
notlist

foo

', - wantAlines: [ - '+1', - '*0*4*6*7+1+1', - '*0*5*6*8+1+1', - '+7', - '+3', - ], - wantText: ['a', '*b', '*c', 'notlist', 'foo'], - noteToSelf: 'Ensure empty P does not induce line attribute marker, wont this break the editor?', - }, - { - description: 'First item being an UL then subsequent being OL will fail', - html: '
  • a
    1. b
    2. c
', - wantAlines: ['+1', '*0*1*2*3+1+1', '*0*4*2*5+1+1'], - wantText: ['a', '*b', '*c'], - noteToSelf: 'Ensure empty P does not induce line attribute marker, wont this break the editor?', - disabled: true, - }, - { - description: 'A single completely empty line break within an ol should NOT reset count', - html: '
  1. should be 1
  2. should be 2
  3. should be 3

', - wantAlines: [], - wantText: ['*should be 1', '*should be 2', '*should be 3'], - noteToSelf: "

should create a line break but not break numbering -- This is what I can't get working!", - disabled: true, - }, - { - description: 'Content outside body should be ignored', - html: 'titleempty
', - wantAlines: ['+5'], - wantText: ['empty'], - }, - { - description: 'Multiple spaces should be preserved', - html: 'Text with more than one space.
', - wantAlines: ['+10'], - wantText: ['Text with more than one space.'], - }, - { - description: 'non-breaking and normal space should be preserved', - html: 'Text with  more   than  one space.
', - wantAlines: ['+10'], - wantText: ['Text with more than one space.'], - }, - { - description: 'Multiple nbsp should be preserved', - html: '  
', - wantAlines: ['+2'], - wantText: [' '], - }, - { - description: 'Multiple nbsp between words ', - html: '  word1  word2   word3
', - wantAlines: ['+m'], - wantText: [' word1 word2 word3'], - }, - { - description: 'A non-breaking space preceded by a normal space', - html: '  word1  word2  word3
', - wantAlines: ['+l'], - wantText: [' word1 word2 word3'], - }, - { - description: 'A non-breaking space followed by a normal space', - html: '  word1  word2  word3
', - wantAlines: ['+l'], - wantText: [' word1 word2 word3'], - }, - { - description: 'Don\'t collapse spaces that follow a newline', - html: 'something
something
', - wantAlines: ['+9', '+m'], - wantText: ['something', ' something'], - }, - { - description: 'Don\'t collapse spaces that follow a empty paragraph', - html: 'something

something
', - wantAlines: ['+9', '', '+m'], - wantText: ['something', '', ' something'], - }, - { - description: 'Don\'t collapse spaces that preceed/follow a newline', - html: 'something
something
', - wantAlines: ['+l', '+m'], - wantText: ['something ', ' something'], - }, - { - description: 'Don\'t collapse spaces that preceed/follow a empty paragraph', - html: 'something

something
', - wantAlines: ['+l', '', '+m'], - wantText: ['something ', '', ' something'], - }, - { - description: 'Don\'t collapse non-breaking spaces that follow a newline', - html: 'something
   something
', - wantAlines: ['+9', '+c'], - wantText: ['something', ' something'], - }, - { - description: 'Don\'t collapse non-breaking spaces that follow a paragraph', - html: 'something

   something
', - wantAlines: ['+9', '', '+c'], - wantText: ['something', '', ' something'], - }, - { - description: 'Preserve all spaces when multiple are present', - html: 'Need more space s !
', - wantAlines: ['+h*1+4+2'], - wantText: ['Need more space s !'], - }, - { - description: 'Newlines and multiple spaces across newlines should be preserved', - html: ` - Need - more - space - s - !
`, - wantAlines: ['+19*1+4+b'], - wantText: ['Need more space s !'], - }, - { - description: 'Multiple new lines at the beginning should be preserved', - html: '

first line

second line
', - wantAlines: ['', '', '', '', '+a', '', '+b'], - wantText: ['', '', '', '', 'first line', '', 'second line'], - }, - { - description: 'A paragraph with multiple lines should not loose spaces when lines are combined', - html: `

-а б в г ґ д е є ж з и і ї й к л м н о -п р с т у ф х ц ч ш щ ю я ь

-`, - wantAlines: ['+1t'], - wantText: ['а б в г ґ д е є ж з и і ї й к л м н о п р с т у ф х ц ч ш щ ю я ь'], - }, - { - description: 'lines in preformatted text should be kept intact', - html: `

-а б в г ґ д е є ж з и і ї й к л м н о

multiple
-lines
-in
-pre
-

п р с т у ф х ц ч ш щ ю я -ь

-`, - wantAlines: ['+11', '+8', '+5', '+2', '+3', '+r'], - wantText: [ - 'а б в г ґ д е є ж з и і ї й к л м н о', - 'multiple', - 'lines', - 'in', - 'pre', - 'п р с т у ф х ц ч ш щ ю я ь', - ], - }, - { - description: 'pre should be on a new line not preceded by a space', - html: `

- 1 -

preline
-
`, - wantAlines: ['+6', '+7'], - wantText: [' 1 ', 'preline'], - }, - { - description: 'Preserve spaces on the beginning and end of a element', - html: 'Need more space s !
', - wantAlines: ['+f*1+3+1'], - wantText: ['Need more space s !'], - }, - { - description: 'Preserve spaces outside elements', - html: 'Need more space s !
', - wantAlines: ['+g*1+1+2'], - wantText: ['Need more space s !'], - }, - { - description: 'Preserve spaces at the end of an element', - html: 'Need more space s !
', - wantAlines: ['+g*1+2+1'], - wantText: ['Need more space s !'], - }, - { - description: 'Preserve spaces at the start of an element', - html: 'Need more space s !
', - wantAlines: ['+f*1+2+2'], - wantText: ['Need more space s !'], - }, -]; - -describe(__filename, function () { - for (const tc of testCases) { - describe(tc.description, function () { - let apool: APool; - let result: { - lines: string[], - lineAttribs: string[], - }; - if (tc.disabled) { - test.skip('If disabled we do not run the test'); - return; - } - - beforeAll(async function () { - - const {window: {document}} = new jsdom.JSDOM(tc.html); - apool = new AttributePool(); - // To reduce test fragility, the attribute pool is seeded with `knownAttribs`, and all - // attributes in `tc.wantAlines` must be in `knownAttribs`. (This guarantees that attribute - // numbers do not change if the attribute processing code changes.) - for (const attrib of knownAttribs) apool.putAttrib(attrib); - for (const aline of tc.wantAlines) { - for (const op of Changeset.deserializeOps(aline)) { - for (const n of attributes.decodeAttribString(op.attribs)) { - assert(n < knownAttribs.length); - } - } - } - const cc = contentcollector.makeContentCollector(true, null, apool); - cc.collectContent(document.body); - result = cc.finish(); - console.log(result); - }); - - it('text matches', async function () { - assert.deepEqual(result.lines, tc.wantText); - }); - - it('alines match', async function () { - assert.deepEqual(result.lineAttribs, tc.wantAlines); - }); - - it('attributes are sorted in canonical order', async function () { - const gotAttribs:string[][][] = []; - const wantAttribs = []; - for (const aline of result.lineAttribs) { - const gotAlineAttribs:string[][] = []; - gotAttribs.push(gotAlineAttribs); - const wantAlineAttribs:string[] = []; - wantAttribs.push(wantAlineAttribs); - for (const op of Changeset.deserializeOps(aline)) { - const gotOpAttribs:string[] = [...attributes.attribsFromString(op.attribs, apool)]; - gotAlineAttribs.push(gotOpAttribs); - wantAlineAttribs.push(attributes.sort([...gotOpAttribs])); - } - } - assert.deepEqual(gotAttribs, wantAttribs); - }); - }); - } -}); diff --git a/src/tests/backend-new/specs/pad_utils.ts b/src/tests/backend-new/specs/pad_utils.ts index bcccdf449..569f49d04 100644 --- a/src/tests/backend-new/specs/pad_utils.ts +++ b/src/tests/backend-new/specs/pad_utils.ts @@ -1,19 +1,19 @@ import {MapArrayType} from "../../../node/types/MapType"; -const {padutils} = require('../../../static/js/pad_utils'); +import padutils from '../../../static/js/pad_utils'; import {describe, it, expect, afterEach, beforeAll} from "vitest"; describe(__filename, function () { describe('warnDeprecated', function () { - const {warnDeprecated} = padutils; + const {warnDeprecatedFlags, warnDeprecated} = padutils; const backups:MapArrayType = {}; beforeAll(async function () { - backups.logger = warnDeprecated.logger; + backups.logger = warnDeprecatedFlags.logger; }); afterEach(async function () { - warnDeprecated.logger = backups.logger; - delete warnDeprecated._rl; // Reset internal rate limiter state. + warnDeprecatedFlags.logger = backups.logger; + delete warnDeprecatedFlags._rl; // Reset internal rate limiter state. }); /*it('includes the stack', async function () { @@ -25,13 +25,13 @@ describe(__filename, function () { it('rate limited', async function () { let got = 0; - warnDeprecated.logger = {warn: () => ++got}; + warnDeprecatedFlags.logger = {warn: () => ++got}; warnDeprecated(); // Initialize internal rate limiter state. - const {period} = warnDeprecated._rl; + const {period} = warnDeprecatedFlags._rl!; got = 0; const testCases = [[0, 1], [0, 1], [period - 1, 1], [period, 2]]; for (const [now, want] of testCases) { // In a loop so that the stack trace is the same. - warnDeprecated._rl.now = () => now; + warnDeprecatedFlags._rl!.now = () => now; warnDeprecated(); expect(got).toEqual(want); } diff --git a/src/tests/backend-new/specs/skiplist.ts b/src/tests/backend-new/specs/skiplist.ts index c1b408e3a..23ad46ae6 100644 --- a/src/tests/backend-new/specs/skiplist.ts +++ b/src/tests/backend-new/specs/skiplist.ts @@ -1,12 +1,13 @@ 'use strict'; -const SkipList = require('ep_etherpad-lite/static/js/skiplist'); +import SkipList from 'ep_etherpad-lite/static/js/skiplist'; import {expect, describe, it} from 'vitest'; describe('skiplist.js', function () { it('rejects null keys', async function () { const skiplist = new SkipList(); for (const key of [undefined, null]) { + // @ts-ignore expect(() => skiplist.push({key})).toThrowError(); } }); diff --git a/src/tests/backend/common.ts b/src/tests/backend/common.ts index 21fb01e2f..4f3937546 100644 --- a/src/tests/backend/common.ts +++ b/src/tests/backend/common.ts @@ -2,12 +2,11 @@ import {MapArrayType} from "../../node/types/MapType"; -const AttributePool = require('../../static/js/AttributePool'); -const apiHandler = require('../../node/handler/APIHandler'); +import AttributePool from '../../static/js/AttributePool'; const assert = require('assert').strict; const io = require('socket.io-client'); const log4js = require('log4js'); -const {padutils} = require('../../static/js/pad_utils'); +import padutils from '../../static/js/pad_utils'; const process = require('process'); const server = require('../../node/server'); const setCookieParser = require('set-cookie-parser'); diff --git a/src/tests/backend/specs/ImportEtherpad.ts b/src/tests/backend/specs/ImportEtherpad.ts index b0a208b4a..b9ac9baaf 100644 --- a/src/tests/backend/specs/ImportEtherpad.ts +++ b/src/tests/backend/specs/ImportEtherpad.ts @@ -8,7 +8,7 @@ const db = require('../../../node/db/DB'); const importEtherpad = require('../../../node/utils/ImportEtherpad'); const padManager = require('../../../node/db/PadManager'); const plugins = require('../../../static/js/pluginfw/plugin_defs'); -const {randomString} = require('../../../static/js/pad_utils'); +import {randomString} from '../../../static/js/pad_utils'; describe(__filename, function () { let padId: string; diff --git a/src/tests/backend/specs/chat.ts b/src/tests/backend/specs/chat.ts index 5070a30a1..62ac97012 100644 --- a/src/tests/backend/specs/chat.ts +++ b/src/tests/backend/specs/chat.ts @@ -3,7 +3,7 @@ import {MapArrayType} from "../../../node/types/MapType"; import {PluginDef} from "../../../node/types/PartType"; -const ChatMessage = require('../../../static/js/ChatMessage'); +import ChatMessage from '../../../static/js/ChatMessage'; const {Pad} = require('../../../node/db/Pad'); const assert = require('assert').strict; const common = require('../common'); @@ -103,10 +103,14 @@ describe(__filename, function () { checkHook('chatNewMessage', ({message}) => { assert(message != null); assert(message instanceof ChatMessage); - assert.equal(message.authorId, authorId); - assert.equal(message.text, this.test!.title); - assert(message.time >= start); - assert(message.time <= Date.now()); + // @ts-ignore + assert.equal(message!.authorId, authorId); + // @ts-ignore + assert.equal(message!.text, this.test!.title); + // @ts-ignore + assert(message!.time >= start); + // @ts-ignore + assert(message!.time <= Date.now()); }), sendChat(socket, {text: this.test!.title}), ]); @@ -153,7 +157,9 @@ describe(__filename, function () { const customMetadata = {foo: this.test!.title}; await Promise.all([ checkHook('chatNewMessage', ({message}) => { + // @ts-ignore message.text = modifiedText; + // @ts-ignore message.customMetadata = customMetadata; }), (async () => { diff --git a/src/tests/backend/specs/contentcollector.ts b/src/tests/backend/specs/contentcollector.ts index 51ae0002f..50178fbae 100644 --- a/src/tests/backend/specs/contentcollector.ts +++ b/src/tests/backend/specs/contentcollector.ts @@ -11,16 +11,17 @@ import {APool} from "../../../node/types/PadType"; -const AttributePool = require('../../../static/js/AttributePool'); +import AttributePool from '../../../static/js/AttributePool'; const Changeset = require('../../../static/js/Changeset'); const assert = require('assert').strict; -const attributes = require('../../../static/js/attributes'); +import attributes from '../../../static/js/attributes'; const contentcollector = require('../../../static/js/contentcollector'); -const jsdom = require('jsdom'); +import jsdom from 'jsdom'; +import {Attribute} from "../../../static/js/types/Attribute"; // All test case `wantAlines` values must only refer to attributes in this list so that the // attribute numbers do not change due to changes in pool insertion order. -const knownAttribs = [ +const knownAttribs: Attribute[] = [ ['insertorder', 'first'], ['italic', 'true'], ['list', 'bullet1'], @@ -336,7 +337,7 @@ pre describe(__filename, function () { for (const tc of testCases) { describe(tc.description, function () { - let apool: APool; + let apool: AttributePool; let result: { lines: string[], lineAttribs: string[], @@ -376,11 +377,12 @@ describe(__filename, function () { for (const aline of result.lineAttribs) { const gotAlineAttribs:string[][] = []; gotAttribs.push(gotAlineAttribs); - const wantAlineAttribs:string[] = []; + const wantAlineAttribs:Attribute[] = []; wantAttribs.push(wantAlineAttribs); for (const op of Changeset.deserializeOps(aline)) { - const gotOpAttribs:string[] = [...attributes.attribsFromString(op.attribs, apool)]; + const gotOpAttribs = [...attributes.attribsFromString(op.attribs, apool)] as unknown as Attribute; gotAlineAttribs.push(gotOpAttribs); + // @ts-ignore wantAlineAttribs.push(attributes.sort([...gotOpAttribs])); } }