From 8fe779b58c4f87aad3bac34146550f6e7b317267 Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Fri, 8 Apr 2022 02:37:06 -0400 Subject: [PATCH] Pad: New `padCopy` hook `dstPad` context property --- CHANGELOG.md | 8 +++++--- doc/api/hooks_server-side.md | 2 +- src/node/db/Pad.js | 22 +++++++++++++++------- 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9657139c0..19a3ab415 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -40,7 +40,7 @@ * New `expressPreSession` server-side hook. * Pad server-side hook changes: - * `padCopy`: New `srcPad` context property. + * `padCopy`: New `srcPad` and `dstPad` context properties. * `padDefaultContent`: New hook. * The `db` property on Pad objects is now public. * New `getAuthorId` server-side hook. @@ -68,8 +68,10 @@ `handleMessage` server-side hooks is deprecated; use the `socket` context property instead. * Pad server-side hook changes: - * `padCopy`: The `originalPad` context property is deprecated; use `srcPad` - instead. + * `padCopy`: + * The `originalPad` context property is deprecated; use `srcPad` instead. + * The `destinationID` context property is deprecated; use `dstPad.id` + instead. * `padCreate`: The `author` context property is deprecated; use the new `authorId` context property instead. * `padUpdate`: The `author` context property is deprecated; use the new diff --git a/doc/api/hooks_server-side.md b/doc/api/hooks_server-side.md index b2b0da8b6..a4e9a9050 100644 --- a/doc/api/hooks_server-side.md +++ b/doc/api/hooks_server-side.md @@ -345,7 +345,7 @@ Order of events when a pad is copied: Context properties: * `srcPad`: The source Pad object. - * `destinationID`: The ID of the pad copied from `originalPad`. + * `dstPad`: The destination Pad object. Usage examples: diff --git a/src/node/db/Pad.js b/src/node/db/Pad.js index a029db53d..ece17961b 100644 --- a/src/node/db/Pad.js +++ b/src/node/db/Pad.js @@ -426,7 +426,7 @@ Pad.prototype.copy = async function (destinationID, force) { }).call(this)); // Initialize the new pad (will update the listAllPads cache) - await padManager.getPad(destinationID, null); + const dstPad = await padManager.getPad(destinationID, null); // let the plugins know the pad was copied await hooks.aCallAll('padCopy', { @@ -434,8 +434,12 @@ Pad.prototype.copy = async function (destinationID, force) { warnDeprecated('padCopy originalPad context property is deprecated; use srcPad instead'); return this.srcPad; }, + get destinationID() { + warnDeprecated('padCopy destinationID context property is deprecated; use dstPad.id instead'); + return this.dstPad.id; + }, srcPad: this, - destinationID, + dstPad, }); return {padID: destinationID}; @@ -503,8 +507,8 @@ Pad.prototype.copyPadWithoutHistory = async function (destinationID, force, auth } // initialize the pad with a new line to avoid getting the defaultText - const newPad = await padManager.getPad(destinationID, '\n', authorId); - newPad.pool = this.pool.clone(); + const dstPad = await padManager.getPad(destinationID, '\n', authorId); + dstPad.pool = this.pool.clone(); const oldAText = this.atext; @@ -513,7 +517,7 @@ Pad.prototype.copyPadWithoutHistory = async function (destinationID, force, auth for (const op of Changeset.opsFromAText(oldAText)) assem.append(op); assem.endDocument(); - // although we have instantiated the newPad with '\n', an additional '\n' is + // although we have instantiated the dstPad with '\n', an additional '\n' is // added internally, so the pad text on the revision 0 is "\n\n" const oldLength = 2; @@ -523,15 +527,19 @@ Pad.prototype.copyPadWithoutHistory = async function (destinationID, force, auth // create a changeset that removes the previous text and add the newText with // all atributes present on the source pad const changeset = Changeset.pack(oldLength, newLength, assem.toString(), newText); - newPad.appendRevision(changeset, authorId); + dstPad.appendRevision(changeset, authorId); await hooks.aCallAll('padCopy', { get originalPad() { warnDeprecated('padCopy originalPad context property is deprecated; use srcPad instead'); return this.srcPad; }, + get destinationID() { + warnDeprecated('padCopy destinationID context property is deprecated; use dstPad.id instead'); + return this.dstPad.id; + }, srcPad: this, - destinationID, + dstPad, }); return {padID: destinationID};