UNPKG

@samepage/internal

Version:

Utilities used across modules - not meant for use by users directly

73 lines 2.96 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const automerge_1 = tslib_1.__importDefault(require("automerge")); const wrapSchema_1 = tslib_1.__importDefault(require("../utils/wrapSchema")); const localAutomergeDb_1 = require("../utils/localAutomergeDb"); const apiClient_1 = tslib_1.__importDefault(require("./apiClient")); const binaryToBase64_1 = tslib_1.__importDefault(require("./binaryToBase64")); const dispatchAppEvent_1 = tslib_1.__importDefault(require("./dispatchAppEvent")); const parseActorId_1 = tslib_1.__importDefault(require("./parseActorId")); const sharePageCommandCalback = ({ getNotebookPageId, encodeState, }) => { return getNotebookPageId() .then(async (notebookPageId) => { if (!notebookPageId) return Promise.reject(new Error(`Failed to detect a page to share`)); const { $body: docInit, ...properties } = await encodeState(notebookPageId); const { actorId } = await (0, parseActorId_1.default)(); const doc = automerge_1.default.from((0, wrapSchema_1.default)(docInit), { actorId: actorId.replace(/-/g, ""), }); (0, localAutomergeDb_1.set)(notebookPageId, doc); const state = automerge_1.default.save(doc); return (0, apiClient_1.default)({ method: "init-shared-page", notebookPageId, state: (0, binaryToBase64_1.default)(state), properties, }) .then(async (r) => { if (r.created) { (0, dispatchAppEvent_1.default)({ type: "log", id: "init-page-success", content: `Successfully initialized shared page! Click on the invite button below to share the page with other notebooks!`, intent: "info", }); } else { (0, dispatchAppEvent_1.default)({ type: "log", id: "samepage-warning", content: "This page is already shared from this notebook", intent: "warning", }); } return { notebookPageId, created: r.created, linkUuid: r.linkUuid, success: true, }; }) .catch((e) => { (0, localAutomergeDb_1.deleteId)(notebookPageId); throw e; }); }) .catch((e) => { const content = `Failed to share page on network: ${e.message}`; (0, dispatchAppEvent_1.default)({ type: "log", intent: "error", id: "init-page-failure", content, }); return { success: false, error: content, }; }); }; exports.default = sharePageCommandCalback; //# sourceMappingURL=sharePageCommandCallback.js.map