@seed-hypermedia/client 0.0.67 → 0.0.69
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/dist/auth.d.ts +79 -0
- package/dist/auth.mjs +265 -0
- package/dist/auto-link.mjs +20 -0
- package/dist/base64.mjs +9 -0
- package/dist/blobs.d.ts +168 -0
- package/dist/blobs.mjs +50 -0
- package/dist/block-diff.d.ts +37 -1
- package/dist/block-diff.mjs +17 -0
- package/dist/blocks-to-markdown.mjs +22 -0
- package/dist/capability.mjs +11 -0
- package/dist/cbor.d.ts +13 -0
- package/dist/cbor.mjs +13 -0
- package/dist/change.mjs +23 -0
- package/dist/chunk-2MD3FXKB.mjs +14 -0
- package/dist/chunk-3TPCJCVO.mjs +24 -0
- package/dist/chunk-52P3E6H6.mjs +459 -0
- package/dist/{chunk-KCR3277A.mjs → chunk-5EWV4OPF.mjs} +1 -1
- package/dist/chunk-CBELM7KX.mjs +29 -0
- package/dist/chunk-DXDF36H5.mjs +289 -0
- package/dist/chunk-EY6RQVAP.mjs +138 -0
- package/dist/chunk-FYUZZ3HT.mjs +199 -0
- package/dist/chunk-GKGWSN6J.mjs +75 -0
- package/dist/chunk-H7L4TI6X.mjs +705 -0
- package/dist/chunk-HBIVZA6C.mjs +102 -0
- package/dist/chunk-I3YUHKYW.mjs +135 -0
- package/dist/chunk-KO4XH5L2.mjs +475 -0
- package/dist/chunk-LYECQKZE.mjs +49 -0
- package/dist/chunk-MKUWQB5W.mjs +297 -0
- package/dist/chunk-N3P7S5YZ.mjs +118 -0
- package/dist/chunk-NPG76WUS.mjs +348 -0
- package/dist/chunk-NX7YFFRX.mjs +124 -0
- package/dist/chunk-OZKQOI2X.mjs +334 -0
- package/dist/chunk-PPAY4WZX.mjs +40 -0
- package/dist/chunk-QS7TPMYT.mjs +411 -0
- package/dist/chunk-S2PVJK2P.mjs +517 -0
- package/dist/chunk-T5BTAJPW.mjs +725 -0
- package/dist/{chunk-EGCUJ6D6.mjs → chunk-UOL3F6XQ.mjs} +0 -12
- package/dist/chunk-VN7F33SS.mjs +108 -0
- package/dist/chunk-W4OGV22Y.mjs +57 -0
- package/dist/chunk-WJQSJ6UM.mjs +159 -0
- package/dist/chunk-XI2LXZM7.mjs +112 -0
- package/dist/chunk-YNVFRCUH.mjs +171 -0
- package/dist/chunk-YZX54L3G.mjs +33 -0
- package/dist/client.mjs +14 -0
- package/dist/comment.mjs +18 -0
- package/dist/contact.mjs +14 -0
- package/dist/document-state.mjs +8 -0
- package/dist/editor-types.mjs +0 -0
- package/dist/editorblock-to-hmblock.mjs +11 -0
- package/dist/encryption.mjs +13 -0
- package/dist/errors.mjs +11 -0
- package/dist/file-to-ipfs.mjs +13 -0
- package/dist/grobid.mjs +11 -0
- package/dist/hm-resolver.mjs +10 -0
- package/dist/hm-types.d.ts +566 -566
- package/dist/hmauth.d.ts +169 -0
- package/dist/hmauth.mjs +52 -0
- package/dist/hmblock-to-editorblock.mjs +12 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.mjs +126 -5006
- package/dist/keyfile.mjs +19 -0
- package/dist/markdown-to-blocks.d.ts +7 -0
- package/dist/markdown-to-blocks.mjs +15 -0
- package/dist/pdf-to-blocks-embedded.mjs +7 -0
- package/dist/pdf-to-blocks.mjs +11 -0
- package/dist/ref.mjs +12 -0
- package/dist/resource-read.mjs +14 -0
- package/dist/signing.mjs +13 -0
- package/dist/tei-to-blocks.mjs +8 -0
- package/dist/unicode.mjs +19 -0
- package/dist/vault-local.mjs +7 -6
- package/dist/vault.mjs +3 -2
- package/package.json +18 -28
- package/src/index.ts +0 -137
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import {
|
|
2
|
+
unpackHmId
|
|
3
|
+
} from "./chunk-SQEUO7FO.mjs";
|
|
4
|
+
|
|
5
|
+
// src/document-state.ts
|
|
6
|
+
async function resolveDocumentState(client, targetId) {
|
|
7
|
+
const unpacked = unpackHmId(targetId);
|
|
8
|
+
if (!unpacked) throw new Error(`Invalid ID: ${targetId}`);
|
|
9
|
+
const changesResp = await client.request("ListChanges", { targetId: unpacked });
|
|
10
|
+
if (!changesResp.changes || changesResp.changes.length === 0) {
|
|
11
|
+
throw new Error(`No changes found for ${targetId}. Document may not exist.`);
|
|
12
|
+
}
|
|
13
|
+
const depthMap = computeDepths(changesResp);
|
|
14
|
+
let genesis = "";
|
|
15
|
+
for (const change of changesResp.changes) {
|
|
16
|
+
if (!change.deps || change.deps.length === 0) {
|
|
17
|
+
genesis = change.id;
|
|
18
|
+
break;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
if (!genesis) {
|
|
22
|
+
throw new Error("Could not find genesis change (change with no deps).");
|
|
23
|
+
}
|
|
24
|
+
const allDeps = /* @__PURE__ */ new Set();
|
|
25
|
+
for (const change of changesResp.changes) {
|
|
26
|
+
if (change.deps) {
|
|
27
|
+
for (const dep of change.deps) {
|
|
28
|
+
allDeps.add(dep);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
const heads = [];
|
|
33
|
+
for (const change of changesResp.changes) {
|
|
34
|
+
if (change.id && !allDeps.has(change.id)) {
|
|
35
|
+
heads.push(change.id);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
if (heads.length === 0) {
|
|
39
|
+
throw new Error("Could not determine document heads.");
|
|
40
|
+
}
|
|
41
|
+
let maxHeadDepth = 0;
|
|
42
|
+
for (const head of heads) {
|
|
43
|
+
const d = depthMap.get(head) ?? 0;
|
|
44
|
+
if (d > maxHeadDepth) maxHeadDepth = d;
|
|
45
|
+
}
|
|
46
|
+
const version = heads.join(".");
|
|
47
|
+
return {
|
|
48
|
+
genesis,
|
|
49
|
+
heads,
|
|
50
|
+
headDepth: maxHeadDepth,
|
|
51
|
+
version
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
function computeDepths(changesResp) {
|
|
55
|
+
const depthMap = /* @__PURE__ */ new Map();
|
|
56
|
+
const depsMap = /* @__PURE__ */ new Map();
|
|
57
|
+
const dependents = /* @__PURE__ */ new Map();
|
|
58
|
+
for (const change of changesResp.changes) {
|
|
59
|
+
const id = change.id;
|
|
60
|
+
const deps = change.deps ?? [];
|
|
61
|
+
depsMap.set(id, deps);
|
|
62
|
+
for (const dep of deps) {
|
|
63
|
+
const existing = dependents.get(dep) ?? [];
|
|
64
|
+
existing.push(id);
|
|
65
|
+
dependents.set(dep, existing);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
const queue = [];
|
|
69
|
+
for (const change of changesResp.changes) {
|
|
70
|
+
const deps = change.deps ?? [];
|
|
71
|
+
if (deps.length === 0) {
|
|
72
|
+
depthMap.set(change.id, 0);
|
|
73
|
+
queue.push(change.id);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
while (queue.length > 0) {
|
|
77
|
+
const current = queue.shift();
|
|
78
|
+
const children = dependents.get(current) ?? [];
|
|
79
|
+
for (const child of children) {
|
|
80
|
+
const childDeps = depsMap.get(child) ?? [];
|
|
81
|
+
let allResolved = true;
|
|
82
|
+
let maxDepDepth = 0;
|
|
83
|
+
for (const dep of childDeps) {
|
|
84
|
+
const d = depthMap.get(dep);
|
|
85
|
+
if (d === void 0) {
|
|
86
|
+
allResolved = false;
|
|
87
|
+
break;
|
|
88
|
+
}
|
|
89
|
+
if (d > maxDepDepth) maxDepDepth = d;
|
|
90
|
+
}
|
|
91
|
+
if (allResolved && !depthMap.has(child)) {
|
|
92
|
+
depthMap.set(child, maxDepDepth + 1);
|
|
93
|
+
queue.push(child);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
return depthMap;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export {
|
|
101
|
+
resolveDocumentState
|
|
102
|
+
};
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createVersionRef
|
|
3
|
+
} from "./chunk-XI2LXZM7.mjs";
|
|
4
|
+
import {
|
|
5
|
+
cborCodec,
|
|
6
|
+
normalizeBytes
|
|
7
|
+
} from "./chunk-YZX54L3G.mjs";
|
|
8
|
+
|
|
9
|
+
// src/change.ts
|
|
10
|
+
import { decode as cborDecode, encode as cborEncode } from "@ipld/dag-cbor";
|
|
11
|
+
import { CID } from "multiformats";
|
|
12
|
+
import * as Block from "multiformats/block";
|
|
13
|
+
import { sha256 } from "multiformats/hashes/sha2";
|
|
14
|
+
function createChangeOps(input) {
|
|
15
|
+
const ts = input.ts ?? BigInt(Date.now());
|
|
16
|
+
const unsigned = {
|
|
17
|
+
type: "Change",
|
|
18
|
+
body: {
|
|
19
|
+
ops: input.ops,
|
|
20
|
+
opCount: input.ops.length
|
|
21
|
+
},
|
|
22
|
+
signer: null,
|
|
23
|
+
ts,
|
|
24
|
+
sig: null
|
|
25
|
+
};
|
|
26
|
+
if (input.genesisCid) {
|
|
27
|
+
unsigned.genesis = input.genesisCid;
|
|
28
|
+
unsigned.deps = input.deps ?? [];
|
|
29
|
+
unsigned.depth = input.depth ?? 1;
|
|
30
|
+
}
|
|
31
|
+
return { unsignedBytes: cborEncode(unsigned), ts };
|
|
32
|
+
}
|
|
33
|
+
async function createChange(unsignedBytes, signer) {
|
|
34
|
+
const change = cborDecode(unsignedBytes);
|
|
35
|
+
const genesis = change.genesis instanceof CID ? change.genesis : null;
|
|
36
|
+
const ts = timestampToNumber(change.ts);
|
|
37
|
+
change.signer = new Uint8Array(await signer.getPublicKey());
|
|
38
|
+
change.sig = new Uint8Array(64);
|
|
39
|
+
change.sig = await signer.sign(cborEncode(change));
|
|
40
|
+
const block = await Block.encode({ value: change, codec: cborCodec, hasher: sha256 });
|
|
41
|
+
return { bytes: block.bytes, cid: block.cid, genesis, ts };
|
|
42
|
+
}
|
|
43
|
+
var signPreparedChange = async (unsignedBytes, signer) => {
|
|
44
|
+
const result = await createChange(unsignedBytes, signer);
|
|
45
|
+
return { signedBytes: result.bytes, cid: result.cid, genesis: result.genesis };
|
|
46
|
+
};
|
|
47
|
+
async function createGenesisChange(signer) {
|
|
48
|
+
const pubKey = await signer.getPublicKey();
|
|
49
|
+
const unsigned = {
|
|
50
|
+
type: "Change",
|
|
51
|
+
signer: new Uint8Array(pubKey),
|
|
52
|
+
sig: new Uint8Array(64),
|
|
53
|
+
ts: BigInt(0)
|
|
54
|
+
};
|
|
55
|
+
unsigned.sig = await signer.sign(cborEncode(unsigned));
|
|
56
|
+
const block = await Block.encode({ value: unsigned, codec: cborCodec, hasher: sha256 });
|
|
57
|
+
return { bytes: block.bytes, cid: block.cid };
|
|
58
|
+
}
|
|
59
|
+
async function createDocumentChangeFromOps(input, signer) {
|
|
60
|
+
const { unsignedBytes, ts } = createChangeOps(input);
|
|
61
|
+
const { bytes, cid } = await createChange(unsignedBytes, signer);
|
|
62
|
+
return { bytes, cid, ts };
|
|
63
|
+
}
|
|
64
|
+
async function createDocumentChange(input, signer) {
|
|
65
|
+
const ops = protoChangesToOps(input.changes);
|
|
66
|
+
const { unsignedBytes } = createChangeOps({ ops, genesisCid: input.genesisCid, deps: input.deps, depth: input.depth });
|
|
67
|
+
const { bytes, cid } = await createChange(unsignedBytes, signer);
|
|
68
|
+
return { bytes, cid };
|
|
69
|
+
}
|
|
70
|
+
function protoChangesToOps(changes) {
|
|
71
|
+
return changes.map((change) => {
|
|
72
|
+
const op = change.op;
|
|
73
|
+
if (!op || !op.case) throw new Error("Invalid change: missing op");
|
|
74
|
+
switch (op.case) {
|
|
75
|
+
case "setMetadata":
|
|
76
|
+
return {
|
|
77
|
+
type: "SetAttributes",
|
|
78
|
+
attrs: [{ key: [op.value.key], value: op.value.value }]
|
|
79
|
+
};
|
|
80
|
+
default:
|
|
81
|
+
throw new Error(
|
|
82
|
+
`Op "${op.case}" is not supported for client-side change creation. Use PrepareDocumentChange + createChange instead.`
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
var RESOURCE_VISIBILITY_PRIVATE = 2;
|
|
88
|
+
function visibilityToCbor(protoVisibility) {
|
|
89
|
+
return protoVisibility === RESOURCE_VISIBILITY_PRIVATE ? "Private" : void 0;
|
|
90
|
+
}
|
|
91
|
+
function timestampToNumber(ts) {
|
|
92
|
+
if (typeof ts === "number") return ts;
|
|
93
|
+
if (typeof ts === "bigint") return Number(ts);
|
|
94
|
+
return void 0;
|
|
95
|
+
}
|
|
96
|
+
async function signDocumentChange(input, signer) {
|
|
97
|
+
const {
|
|
98
|
+
bytes: signedBytes,
|
|
99
|
+
cid: changeCid,
|
|
100
|
+
genesis: changeGenesis,
|
|
101
|
+
ts: changeTs
|
|
102
|
+
} = await createChange(input.unsignedChange, signer);
|
|
103
|
+
const effectiveGenesis = input.genesis || (changeGenesis ? changeGenesis.toString() : changeCid.toString());
|
|
104
|
+
const effectiveGeneration = input.generation != null ? Number(input.generation) : changeTs ?? Date.now();
|
|
105
|
+
const refBlobs = await createVersionRef(
|
|
106
|
+
{
|
|
107
|
+
space: input.account,
|
|
108
|
+
path: input.path ?? "",
|
|
109
|
+
genesis: effectiveGenesis,
|
|
110
|
+
version: changeCid.toString(),
|
|
111
|
+
generation: effectiveGeneration,
|
|
112
|
+
capability: input.capability,
|
|
113
|
+
visibility: visibilityToCbor(input.visibility),
|
|
114
|
+
ts: changeTs
|
|
115
|
+
},
|
|
116
|
+
signer
|
|
117
|
+
);
|
|
118
|
+
return {
|
|
119
|
+
changeCid,
|
|
120
|
+
publishInput: {
|
|
121
|
+
blobs: [{ data: normalizeBytes(signedBytes), cid: changeCid.toString() }, ...refBlobs.blobs]
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export {
|
|
127
|
+
createChangeOps,
|
|
128
|
+
createChange,
|
|
129
|
+
signPreparedChange,
|
|
130
|
+
createGenesisChange,
|
|
131
|
+
createDocumentChangeFromOps,
|
|
132
|
+
createDocumentChange,
|
|
133
|
+
visibilityToCbor,
|
|
134
|
+
signDocumentChange
|
|
135
|
+
};
|
|
@@ -0,0 +1,475 @@
|
|
|
1
|
+
import {
|
|
2
|
+
isSurrogate
|
|
3
|
+
} from "./chunk-SQEUO7FO.mjs";
|
|
4
|
+
|
|
5
|
+
// src/hmblock-to-editorblock.ts
|
|
6
|
+
function toEditorBlockType(hmBlockType) {
|
|
7
|
+
if (hmBlockType === "Heading") return "heading";
|
|
8
|
+
if (hmBlockType === "Paragraph") return "paragraph";
|
|
9
|
+
if (hmBlockType === "Code") return "code-block";
|
|
10
|
+
if (hmBlockType === "Math") return "math";
|
|
11
|
+
if (hmBlockType === "Image") return "image";
|
|
12
|
+
if (hmBlockType === "Video") return "video";
|
|
13
|
+
if (hmBlockType === "File") return "file";
|
|
14
|
+
if (hmBlockType === "Button") return "button";
|
|
15
|
+
if (hmBlockType === "Embed") return "embed";
|
|
16
|
+
if (hmBlockType === "WebEmbed") return "web-embed";
|
|
17
|
+
if (hmBlockType === "Query") return "query";
|
|
18
|
+
if (hmBlockType === "Table") return "table";
|
|
19
|
+
if (hmBlockType === "TableRow") return "tableRow";
|
|
20
|
+
if (hmBlockType === "TableColumn") return "tableColumn";
|
|
21
|
+
return "unknown";
|
|
22
|
+
}
|
|
23
|
+
function hmBlocksToEditorContent(blocks, opts = { level: 1 }) {
|
|
24
|
+
const childRecursiveOpts = {
|
|
25
|
+
level: opts.level || 0
|
|
26
|
+
};
|
|
27
|
+
const parentIsTable = opts.parentType === "Table";
|
|
28
|
+
let rowIdx = 0;
|
|
29
|
+
let colIdx = 0;
|
|
30
|
+
return blocks.filter((hmBlock) => {
|
|
31
|
+
var _a, _b;
|
|
32
|
+
const t = (_a = hmBlock.block) == null ? void 0 : _a.type;
|
|
33
|
+
if ((t === "TableRow" || t === "TableColumn") && !parentIsTable) {
|
|
34
|
+
console.warn(
|
|
35
|
+
`[hmBlocksToEditorContent] dropping orphan ${t} block (parent type: ${opts.parentType ?? "root"})`,
|
|
36
|
+
(_b = hmBlock.block) == null ? void 0 : _b.id
|
|
37
|
+
);
|
|
38
|
+
return false;
|
|
39
|
+
}
|
|
40
|
+
return true;
|
|
41
|
+
}).map((hmBlock) => {
|
|
42
|
+
var _a, _b, _c, _d, _e;
|
|
43
|
+
let positionIdx;
|
|
44
|
+
if (parentIsTable) {
|
|
45
|
+
const t = (_a = hmBlock.block) == null ? void 0 : _a.type;
|
|
46
|
+
if (t === "TableRow") positionIdx = rowIdx++;
|
|
47
|
+
else if (t === "TableColumn") positionIdx = colIdx++;
|
|
48
|
+
}
|
|
49
|
+
let res = hmBlock.block ? hmBlockToEditorBlock(hmBlock.block) : null;
|
|
50
|
+
if (res && positionIdx !== void 0 && positionIdx > 0) {
|
|
51
|
+
const props = res.props;
|
|
52
|
+
if (props && "isHeader" in props) {
|
|
53
|
+
delete props.isHeader;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
if (res && ((_b = hmBlock.children) == null ? void 0 : _b.length)) {
|
|
57
|
+
const childrenType = (_d = ((_c = hmBlock.block) == null ? void 0 : _c.attributes) || {}) == null ? void 0 : _d.childrenType;
|
|
58
|
+
const validChildrenType = childrenType === "Group" || childrenType === "Ordered" || childrenType === "Unordered" || childrenType === "Blockquote" || childrenType === "Grid" ? childrenType : "Group";
|
|
59
|
+
res.children = hmBlocksToEditorContent(hmBlock.children, {
|
|
60
|
+
level: childRecursiveOpts.level ? childRecursiveOpts.level + 1 : 1,
|
|
61
|
+
childrenType: validChildrenType,
|
|
62
|
+
parentType: (_e = hmBlock.block) == null ? void 0 : _e.type
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
return res;
|
|
66
|
+
}).filter((block) => block !== null);
|
|
67
|
+
}
|
|
68
|
+
function hmBlockToEditorBlock(block) {
|
|
69
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n;
|
|
70
|
+
const blockType = toEditorBlockType(block.type);
|
|
71
|
+
if (blockType === "unknown") {
|
|
72
|
+
return {
|
|
73
|
+
id: block.id,
|
|
74
|
+
type: "unknown",
|
|
75
|
+
content: [],
|
|
76
|
+
props: {
|
|
77
|
+
revision: block.revision,
|
|
78
|
+
originalType: block.type,
|
|
79
|
+
originalData: JSON.stringify(block)
|
|
80
|
+
},
|
|
81
|
+
children: []
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
let out = {
|
|
85
|
+
id: block.id,
|
|
86
|
+
type: blockType,
|
|
87
|
+
content: [],
|
|
88
|
+
props: {
|
|
89
|
+
revision: block.revision
|
|
90
|
+
},
|
|
91
|
+
children: []
|
|
92
|
+
};
|
|
93
|
+
const attributes = block.attributes || {};
|
|
94
|
+
if ("childrenType" in attributes && attributes.childrenType) {
|
|
95
|
+
const childrenType = attributes.childrenType;
|
|
96
|
+
if (childrenType === "Group" || childrenType === "Ordered" || childrenType === "Unordered" || childrenType === "Blockquote" || childrenType === "Grid") {
|
|
97
|
+
;
|
|
98
|
+
out.props.childrenType = childrenType;
|
|
99
|
+
}
|
|
100
|
+
if (childrenType === "Grid" && attributes.columnCount != null) {
|
|
101
|
+
;
|
|
102
|
+
out.props.columnCount = String(attributes.columnCount);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
if (["code-block", "video", "image", "file", "button", "embed", "web-embed", "math", "nostr"].includes(blockType)) {
|
|
106
|
+
if (block.link) {
|
|
107
|
+
;
|
|
108
|
+
out.props.url = block.link;
|
|
109
|
+
}
|
|
110
|
+
if (blockType == "code-block") {
|
|
111
|
+
out.type = "code-block";
|
|
112
|
+
}
|
|
113
|
+
if (block.attributes) {
|
|
114
|
+
Object.entries(block.attributes).forEach(([key, value]) => {
|
|
115
|
+
if (value !== void 0) {
|
|
116
|
+
if (key == "width" || key == "size") {
|
|
117
|
+
if (typeof value == "number") {
|
|
118
|
+
;
|
|
119
|
+
out.props[key] = blockType === "image" && key === "width" && value > 0 && value <= 100 ? `${value}%` : String(value);
|
|
120
|
+
}
|
|
121
|
+
} else if (typeof value == "boolean") {
|
|
122
|
+
;
|
|
123
|
+
out.props[key] = String(value);
|
|
124
|
+
} else {
|
|
125
|
+
;
|
|
126
|
+
out.props[key] = value;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
if (block.type === "Paragraph") {
|
|
133
|
+
const columnId = (_a = block.attributes) == null ? void 0 : _a.columnId;
|
|
134
|
+
if (typeof columnId === "string" && columnId) {
|
|
135
|
+
;
|
|
136
|
+
out.props.columnId = columnId;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
if (block.type === "TableColumn") {
|
|
140
|
+
const width = (_b = block.attributes) == null ? void 0 : _b.width;
|
|
141
|
+
if (typeof width === "number") {
|
|
142
|
+
;
|
|
143
|
+
out.props.width = String(width);
|
|
144
|
+
}
|
|
145
|
+
const isHeader = (_c = block.attributes) == null ? void 0 : _c.isHeader;
|
|
146
|
+
if (isHeader === true) {
|
|
147
|
+
;
|
|
148
|
+
out.props.isHeader = true;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
if (block.type === "TableRow") {
|
|
152
|
+
const isHeader = (_d = block.attributes) == null ? void 0 : _d.isHeader;
|
|
153
|
+
if (isHeader === true) {
|
|
154
|
+
;
|
|
155
|
+
out.props.isHeader = true;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
if (block.type === "Query") {
|
|
159
|
+
const queryProps = out.props;
|
|
160
|
+
queryProps.style = (_e = block.attributes) == null ? void 0 : _e.style;
|
|
161
|
+
queryProps.columnCount = String(((_f = block.attributes) == null ? void 0 : _f.columnCount) || "");
|
|
162
|
+
queryProps.queryIncludes = JSON.stringify(((_h = (_g = block.attributes) == null ? void 0 : _g.query) == null ? void 0 : _h.includes) || []);
|
|
163
|
+
queryProps.querySort = JSON.stringify(((_j = (_i = block.attributes) == null ? void 0 : _i.query) == null ? void 0 : _j.sort) || {});
|
|
164
|
+
queryProps.banner = ((_k = block.attributes) == null ? void 0 : _k.banner) ? "true" : "false";
|
|
165
|
+
queryProps.queryLimit = String(((_m = (_l = block.attributes) == null ? void 0 : _l.query) == null ? void 0 : _m.limit) || "");
|
|
166
|
+
if ((_n = block.attributes) == null ? void 0 : _n.table) queryProps.tableConfig = JSON.stringify(block.attributes.table);
|
|
167
|
+
}
|
|
168
|
+
const blockText = block.text || "";
|
|
169
|
+
const leaves = out.content;
|
|
170
|
+
let leaf = null;
|
|
171
|
+
let inlineBlockContent = null;
|
|
172
|
+
let textStart = 0;
|
|
173
|
+
let i = 0;
|
|
174
|
+
const stopPoint = block.text ? block.text.length - 1 : 0;
|
|
175
|
+
let pos = 0;
|
|
176
|
+
const leafAnnotations = /* @__PURE__ */ new Set();
|
|
177
|
+
if (blockText == "") {
|
|
178
|
+
leaves.push({ type: "text", text: blockText, styles: {} });
|
|
179
|
+
return out;
|
|
180
|
+
}
|
|
181
|
+
while (i < blockText.length) {
|
|
182
|
+
const inlineEmbed = findInlineEmbedStartingAt(pos);
|
|
183
|
+
if (inlineEmbed) {
|
|
184
|
+
if (leaf) {
|
|
185
|
+
finishLeaf(textStart, i);
|
|
186
|
+
leaf = null;
|
|
187
|
+
}
|
|
188
|
+
if (inlineBlockContent) {
|
|
189
|
+
leaves.push(inlineBlockContent);
|
|
190
|
+
inlineBlockContent = null;
|
|
191
|
+
}
|
|
192
|
+
leaves.push({
|
|
193
|
+
type: "inline-embed",
|
|
194
|
+
styles: {},
|
|
195
|
+
link: inlineEmbed.link
|
|
196
|
+
});
|
|
197
|
+
skipToCodepoint(inlineEmbed.end);
|
|
198
|
+
textStart = i;
|
|
199
|
+
leafAnnotations.clear();
|
|
200
|
+
if (i >= blockText.length) return out;
|
|
201
|
+
continue;
|
|
202
|
+
}
|
|
203
|
+
let ul = 1;
|
|
204
|
+
let annotationsChanged = trackPosAnnotations(pos);
|
|
205
|
+
let surrogate = isSurrogate(blockText, i);
|
|
206
|
+
if (surrogate) {
|
|
207
|
+
ul++;
|
|
208
|
+
let onlyOneSurrogate = pos + ul;
|
|
209
|
+
if (onlyOneSurrogate == blockText.length) {
|
|
210
|
+
if (!leaf) {
|
|
211
|
+
startLeaf(leafAnnotations);
|
|
212
|
+
}
|
|
213
|
+
finishLeaf(textStart, i + 2);
|
|
214
|
+
if (inlineBlockContent) {
|
|
215
|
+
const lastLeaf = leaves[leaves.length - 1];
|
|
216
|
+
if (lastLeaf && !isText(lastLeaf)) {
|
|
217
|
+
}
|
|
218
|
+
leaves.push(inlineBlockContent);
|
|
219
|
+
inlineBlockContent = null;
|
|
220
|
+
}
|
|
221
|
+
return out;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
if (stopPoint < 0) {
|
|
225
|
+
console.warn("STOP IS LESS THAN ZERO", block);
|
|
226
|
+
}
|
|
227
|
+
if (i == stopPoint) {
|
|
228
|
+
if (annotationsChanged) {
|
|
229
|
+
if (leaf) {
|
|
230
|
+
finishLeaf(textStart, i);
|
|
231
|
+
}
|
|
232
|
+
startLeaf(leafAnnotations);
|
|
233
|
+
} else {
|
|
234
|
+
startLeaf(leafAnnotations);
|
|
235
|
+
}
|
|
236
|
+
finishLeaf(textStart, i + 1);
|
|
237
|
+
if (inlineBlockContent) {
|
|
238
|
+
const lastLeaf = leaves[leaves.length - 1];
|
|
239
|
+
if (lastLeaf && !isText(lastLeaf)) {
|
|
240
|
+
}
|
|
241
|
+
leaves.push(inlineBlockContent);
|
|
242
|
+
inlineBlockContent = null;
|
|
243
|
+
}
|
|
244
|
+
return out;
|
|
245
|
+
}
|
|
246
|
+
if (!leaf) {
|
|
247
|
+
startLeaf(leafAnnotations);
|
|
248
|
+
advance(ul);
|
|
249
|
+
continue;
|
|
250
|
+
}
|
|
251
|
+
if (annotationsChanged) {
|
|
252
|
+
finishLeaf(textStart, i);
|
|
253
|
+
startLeaf(leafAnnotations);
|
|
254
|
+
}
|
|
255
|
+
advance(ul);
|
|
256
|
+
if (i == blockText.length) {
|
|
257
|
+
finishLeaf(textStart, i);
|
|
258
|
+
return out;
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
throw Error("BUG: should not get here");
|
|
262
|
+
function advance(codeUnits) {
|
|
263
|
+
pos++;
|
|
264
|
+
i += codeUnits;
|
|
265
|
+
}
|
|
266
|
+
function startLeaf(posAnnotations) {
|
|
267
|
+
var _a2, _b2;
|
|
268
|
+
const newLeaf = {
|
|
269
|
+
type: "text",
|
|
270
|
+
text: "",
|
|
271
|
+
styles: {}
|
|
272
|
+
};
|
|
273
|
+
leaf = newLeaf;
|
|
274
|
+
let linkAnnotation = null;
|
|
275
|
+
for (const l of Array.from(posAnnotations)) {
|
|
276
|
+
const annotationData = l;
|
|
277
|
+
if (annotationData.type === "Link") {
|
|
278
|
+
linkAnnotation = {
|
|
279
|
+
type: "Link",
|
|
280
|
+
href: annotationData.link || ""
|
|
281
|
+
};
|
|
282
|
+
}
|
|
283
|
+
if (annotationData.type === "Embed") {
|
|
284
|
+
linkAnnotation = {
|
|
285
|
+
type: "Embed",
|
|
286
|
+
link: annotationData.link || ""
|
|
287
|
+
};
|
|
288
|
+
}
|
|
289
|
+
if (["Bold", "Italic", "Strike", "Underline", "Code", "Range"].includes(annotationData.type)) {
|
|
290
|
+
const styleKey = annotationData.type.toLowerCase();
|
|
291
|
+
newLeaf.styles[styleKey] = true;
|
|
292
|
+
}
|
|
293
|
+
if (annotationData.type === "TextColor") {
|
|
294
|
+
const attrs = annotationData.attributes;
|
|
295
|
+
const color = (attrs == null ? void 0 : attrs.value) ?? (attrs == null ? void 0 : attrs.color);
|
|
296
|
+
if (typeof color === "string" && color) {
|
|
297
|
+
;
|
|
298
|
+
newLeaf.styles.textColor = color;
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
if (annotationData.type === "BackgroundColor") {
|
|
302
|
+
const attrs = annotationData.attributes;
|
|
303
|
+
const color = (attrs == null ? void 0 : attrs.value) ?? (attrs == null ? void 0 : attrs.color);
|
|
304
|
+
if (typeof color === "string" && color) {
|
|
305
|
+
;
|
|
306
|
+
newLeaf.styles.backgroundColor = color;
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
if (annotationData.type === "TextSize") {
|
|
310
|
+
const size = (_a2 = annotationData.attributes) == null ? void 0 : _a2.value;
|
|
311
|
+
if (typeof size === "string" && size) {
|
|
312
|
+
;
|
|
313
|
+
newLeaf.styles.textSize = size;
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
if (annotationData.type === "TextFamily") {
|
|
317
|
+
const family = (_b2 = annotationData.attributes) == null ? void 0 : _b2.value;
|
|
318
|
+
if (typeof family === "string" && family) {
|
|
319
|
+
;
|
|
320
|
+
newLeaf.styles.textFamily = family;
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
if (linkAnnotation) {
|
|
325
|
+
if (linkAnnotation.type === "Embed") {
|
|
326
|
+
leaves.push({
|
|
327
|
+
type: "inline-embed",
|
|
328
|
+
styles: {},
|
|
329
|
+
link: linkAnnotation.link || ""
|
|
330
|
+
});
|
|
331
|
+
textStart = i + 1;
|
|
332
|
+
} else if (inlineBlockContent) {
|
|
333
|
+
if (linkChangedIdentity(linkAnnotation)) {
|
|
334
|
+
leaves.push(inlineBlockContent);
|
|
335
|
+
if (linkAnnotation.type === "Link") {
|
|
336
|
+
inlineBlockContent = {
|
|
337
|
+
type: "link",
|
|
338
|
+
content: [],
|
|
339
|
+
href: linkAnnotation.href || ""
|
|
340
|
+
};
|
|
341
|
+
} else {
|
|
342
|
+
inlineBlockContent = {
|
|
343
|
+
type: "inline-embed",
|
|
344
|
+
styles: {},
|
|
345
|
+
link: linkAnnotation.link || ""
|
|
346
|
+
};
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
} else {
|
|
350
|
+
if (linkAnnotation.type === "Link") {
|
|
351
|
+
inlineBlockContent = {
|
|
352
|
+
type: "link",
|
|
353
|
+
content: [],
|
|
354
|
+
href: linkAnnotation.href || ""
|
|
355
|
+
};
|
|
356
|
+
} else {
|
|
357
|
+
inlineBlockContent = {
|
|
358
|
+
type: "inline-embed",
|
|
359
|
+
styles: {},
|
|
360
|
+
link: linkAnnotation.link || ""
|
|
361
|
+
};
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
} else {
|
|
365
|
+
if (inlineBlockContent) {
|
|
366
|
+
leaves.push(inlineBlockContent);
|
|
367
|
+
inlineBlockContent = null;
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
function linkChangedIdentity(annotation) {
|
|
372
|
+
if (!inlineBlockContent) return false;
|
|
373
|
+
let currentLink = inlineBlockContent.link || inlineBlockContent.href;
|
|
374
|
+
return currentLink != annotation.link && currentLink != annotation.href;
|
|
375
|
+
}
|
|
376
|
+
function finishLeaf(low, high) {
|
|
377
|
+
let newValue = blockText.substring(low, high);
|
|
378
|
+
if (leaf) leaf.text = newValue;
|
|
379
|
+
textStart = high;
|
|
380
|
+
if (inlineBlockContent) {
|
|
381
|
+
if (leaf && inlineBlockContent.type == "link") {
|
|
382
|
+
;
|
|
383
|
+
inlineBlockContent.content.push(leaf);
|
|
384
|
+
} else if (inlineBlockContent.type == "inline-embed" && leaf) {
|
|
385
|
+
} else if (leaf) {
|
|
386
|
+
const typedLeaf = {
|
|
387
|
+
type: "text",
|
|
388
|
+
text: "",
|
|
389
|
+
styles: leaf.styles
|
|
390
|
+
};
|
|
391
|
+
inlineBlockContent.content.push(typedLeaf);
|
|
392
|
+
}
|
|
393
|
+
} else {
|
|
394
|
+
if (leaf && !(leaf.type === "text" && leaf.text === "" && Object.keys(leaf.styles).length === 0)) {
|
|
395
|
+
leaves.push(leaf);
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
function trackPosAnnotations(pos2) {
|
|
400
|
+
let annotationsChanged = false;
|
|
401
|
+
if (!block.annotations) {
|
|
402
|
+
return false;
|
|
403
|
+
}
|
|
404
|
+
const blockAnnotations = block.annotations;
|
|
405
|
+
blockAnnotations.forEach((l) => {
|
|
406
|
+
let spanIdx = annotationContains(l, pos2);
|
|
407
|
+
if (spanIdx === -1) {
|
|
408
|
+
if (leafAnnotations.delete(l)) {
|
|
409
|
+
annotationsChanged = true;
|
|
410
|
+
}
|
|
411
|
+
return;
|
|
412
|
+
}
|
|
413
|
+
if (leafAnnotations.has(l)) {
|
|
414
|
+
return;
|
|
415
|
+
}
|
|
416
|
+
leafAnnotations.add(l);
|
|
417
|
+
annotationsChanged = true;
|
|
418
|
+
});
|
|
419
|
+
return annotationsChanged;
|
|
420
|
+
}
|
|
421
|
+
function findInlineEmbedStartingAt(pos2) {
|
|
422
|
+
var _a2, _b2;
|
|
423
|
+
const blockAnnotations = block.annotations;
|
|
424
|
+
if (!blockAnnotations) return null;
|
|
425
|
+
for (const annotation of blockAnnotations) {
|
|
426
|
+
const annotationData = annotation;
|
|
427
|
+
if (annotationData.type !== "Embed") continue;
|
|
428
|
+
const spanIndex = ((_a2 = annotationData.starts) == null ? void 0 : _a2.findIndex((start) => start === pos2)) ?? -1;
|
|
429
|
+
if (spanIndex === -1) continue;
|
|
430
|
+
const end = (_b2 = annotationData.ends) == null ? void 0 : _b2[spanIndex];
|
|
431
|
+
if (typeof end !== "number" || end <= pos2) continue;
|
|
432
|
+
return { link: annotationData.link || "", end };
|
|
433
|
+
}
|
|
434
|
+
return null;
|
|
435
|
+
}
|
|
436
|
+
function skipToCodepoint(end) {
|
|
437
|
+
while (pos < end && i < blockText.length) {
|
|
438
|
+
i += isSurrogate(blockText, i) ? 2 : 1;
|
|
439
|
+
pos++;
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
function annotationContains(annotation, pos) {
|
|
444
|
+
let low = 0;
|
|
445
|
+
let high = annotation.starts.length - 1;
|
|
446
|
+
let mid = 0;
|
|
447
|
+
while (low <= high) {
|
|
448
|
+
mid = Math.floor((low + high) / 2);
|
|
449
|
+
const endAtMid = annotation.ends[mid];
|
|
450
|
+
if (endAtMid === void 0) break;
|
|
451
|
+
if (endAtMid <= pos) {
|
|
452
|
+
low = mid + 1;
|
|
453
|
+
} else {
|
|
454
|
+
high = mid - 1;
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
if (low == annotation.starts.length) {
|
|
458
|
+
return -1;
|
|
459
|
+
}
|
|
460
|
+
const startAtLow = annotation.starts[low];
|
|
461
|
+
const endAtLow = annotation.ends[low];
|
|
462
|
+
if (startAtLow !== void 0 && endAtLow !== void 0 && startAtLow <= pos && pos < endAtLow) {
|
|
463
|
+
return low;
|
|
464
|
+
}
|
|
465
|
+
return -1;
|
|
466
|
+
}
|
|
467
|
+
function isText(entry) {
|
|
468
|
+
return (entry == null ? void 0 : entry.type) && entry.type == "text" && typeof entry.text == "string";
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
export {
|
|
472
|
+
hmBlocksToEditorContent,
|
|
473
|
+
hmBlockToEditorBlock,
|
|
474
|
+
annotationContains
|
|
475
|
+
};
|