@liveblocks/core 1.8.0 → 1.8.2

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/dist/index.mjs CHANGED
@@ -6,7 +6,7 @@ var __export = (target, all) => {
6
6
 
7
7
  // src/version.ts
8
8
  var PKG_NAME = "@liveblocks/core";
9
- var PKG_VERSION = "1.8.0";
9
+ var PKG_VERSION = "1.8.2";
10
10
  var PKG_FORMAT = "esm";
11
11
 
12
12
  // src/dupe-detection.ts
@@ -3065,7 +3065,7 @@ var LiveList = class _LiveList extends AbstractCrdt {
3065
3065
  this._pool?.assertStorageIsWritable();
3066
3066
  if (index < 0 || index >= this._items.length) {
3067
3067
  throw new Error(
3068
- `Cannot delete list item at index "${index}". index should be between 0 and ${this._items.length - 1}`
3068
+ `Cannot delete list item at index "${index}". index should be between 0 and ${this._items.length - 1}`
3069
3069
  );
3070
3070
  }
3071
3071
  const item = this._items[index];
@@ -5751,7 +5751,9 @@ ${Array.from(traces).join("\n\n")}`
5751
5751
  return returnValue;
5752
5752
  }
5753
5753
  function pauseHistory() {
5754
- context.pausedHistory = [];
5754
+ if (context.pausedHistory === null) {
5755
+ context.pausedHistory = [];
5756
+ }
5755
5757
  }
5756
5758
  function resumeHistory() {
5757
5759
  const historyOps = context.pausedHistory;
@@ -6168,10 +6170,9 @@ function toPlainLson(lson) {
6168
6170
  return {
6169
6171
  liveblocksType: "LiveObject",
6170
6172
  data: Object.fromEntries(
6171
- Object.entries(lson.toObject()).map(([key, value]) => [
6172
- key,
6173
- value ? toPlainLson(value) : ""
6174
- ])
6173
+ Object.entries(lson.toObject()).flatMap(
6174
+ ([key, value]) => value !== void 0 ? [[key, toPlainLson(value)]] : []
6175
+ )
6175
6176
  )
6176
6177
  };
6177
6178
  } else if (lson instanceof LiveMap) {
@@ -6753,6 +6754,315 @@ function stringify(object, ...args) {
6753
6754
  return JSON.stringify(sortedObject, ...args);
6754
6755
  }
6755
6756
 
6757
+ // src/comments/comment-body.ts
6758
+ function isCommentBodyParagraph(element) {
6759
+ return "type" in element && element.type === "mention";
6760
+ }
6761
+ function isCommentBodyText(element) {
6762
+ return "text" in element && typeof element.text === "string";
6763
+ }
6764
+ function isCommentBodyMention(element) {
6765
+ return "type" in element && element.type === "mention";
6766
+ }
6767
+ function isCommentBodyLink(element) {
6768
+ return "type" in element && element.type === "link";
6769
+ }
6770
+ var commentBodyElementsGuards = {
6771
+ paragraph: isCommentBodyParagraph,
6772
+ text: isCommentBodyText,
6773
+ link: isCommentBodyLink,
6774
+ mention: isCommentBodyMention
6775
+ };
6776
+ var commentBodyElementsTypes = {
6777
+ paragraph: "block",
6778
+ text: "inline",
6779
+ link: "inline",
6780
+ mention: "inline"
6781
+ };
6782
+ function traverseCommentBody(body, elementOrVisitor, possiblyVisitor) {
6783
+ if (!body || !body?.content) {
6784
+ return;
6785
+ }
6786
+ const element = typeof elementOrVisitor === "string" ? elementOrVisitor : void 0;
6787
+ const type = element ? commentBodyElementsTypes[element] : "all";
6788
+ const guard = element ? commentBodyElementsGuards[element] : () => true;
6789
+ const visitor = typeof elementOrVisitor === "function" ? elementOrVisitor : possiblyVisitor;
6790
+ for (const block of body.content) {
6791
+ if (type === "all" || type === "block") {
6792
+ if (guard(block)) {
6793
+ visitor?.(block);
6794
+ }
6795
+ }
6796
+ if (type === "all" || type === "inline") {
6797
+ for (const inline of block.children) {
6798
+ if (guard(inline)) {
6799
+ visitor?.(inline);
6800
+ }
6801
+ }
6802
+ }
6803
+ }
6804
+ }
6805
+ function getMentionedIdsFromCommentBody(body) {
6806
+ const mentionedIds = /* @__PURE__ */ new Set();
6807
+ traverseCommentBody(
6808
+ body,
6809
+ "mention",
6810
+ (mention) => mentionedIds.add(mention.id)
6811
+ );
6812
+ return Array.from(mentionedIds);
6813
+ }
6814
+ async function resolveUsersInCommentBody(body, resolveUsers) {
6815
+ const resolvedUsers = /* @__PURE__ */ new Map();
6816
+ if (!resolveUsers) {
6817
+ return resolvedUsers;
6818
+ }
6819
+ const userIds = getMentionedIdsFromCommentBody(body);
6820
+ const users = await resolveUsers({
6821
+ userIds
6822
+ });
6823
+ for (const [index, userId] of userIds.entries()) {
6824
+ const user = users?.[index];
6825
+ if (user) {
6826
+ resolvedUsers.set(userId, user);
6827
+ }
6828
+ }
6829
+ return resolvedUsers;
6830
+ }
6831
+ var htmlEscapables = {
6832
+ "&": "&amp;",
6833
+ "<": "&lt;",
6834
+ ">": "&gt;",
6835
+ '"': "&quot;",
6836
+ "'": "&#39;"
6837
+ };
6838
+ var htmlEscapablesRegex = new RegExp(
6839
+ Object.keys(htmlEscapables).map((entity) => `\\${entity}`).join("|"),
6840
+ "g"
6841
+ );
6842
+ function htmlSafe(value) {
6843
+ return new HtmlSafeString([String(value)], []);
6844
+ }
6845
+ function joinHtml(strings) {
6846
+ if (strings.length <= 0) {
6847
+ return new HtmlSafeString([""], []);
6848
+ }
6849
+ return new HtmlSafeString(
6850
+ ["", ...Array(strings.length - 1).fill(""), ""],
6851
+ strings
6852
+ );
6853
+ }
6854
+ function escapeHtml(value) {
6855
+ if (value instanceof HtmlSafeString) {
6856
+ return value.toString();
6857
+ }
6858
+ if (Array.isArray(value)) {
6859
+ return joinHtml(value).toString();
6860
+ }
6861
+ return String(value).replace(
6862
+ htmlEscapablesRegex,
6863
+ (character) => htmlEscapables[character]
6864
+ );
6865
+ }
6866
+ var HtmlSafeString = class {
6867
+ constructor(strings, values) {
6868
+ this._strings = strings;
6869
+ this._values = values;
6870
+ }
6871
+ toString() {
6872
+ return this._strings.reduce((result, str, i) => {
6873
+ return result + escapeHtml(nn(this._values[i - 1])) + str;
6874
+ });
6875
+ }
6876
+ };
6877
+ function html(strings, ...values) {
6878
+ return new HtmlSafeString(strings, values);
6879
+ }
6880
+ var markdownEscapables = {
6881
+ _: "\\_",
6882
+ "*": "\\*",
6883
+ "#": "\\#",
6884
+ "`": "\\`",
6885
+ "~": "\\~",
6886
+ "!": "\\!",
6887
+ "|": "\\|",
6888
+ "(": "\\(",
6889
+ ")": "\\)",
6890
+ "{": "\\{",
6891
+ "}": "\\}",
6892
+ "[": "\\[",
6893
+ "]": "\\]"
6894
+ };
6895
+ var markdownEscapablesRegex = new RegExp(
6896
+ Object.keys(markdownEscapables).map((entity) => `\\${entity}`).join("|"),
6897
+ "g"
6898
+ );
6899
+ function joinMarkdown(strings) {
6900
+ if (strings.length <= 0) {
6901
+ return new MarkdownSafeString([""], []);
6902
+ }
6903
+ return new MarkdownSafeString(
6904
+ ["", ...Array(strings.length - 1).fill(""), ""],
6905
+ strings
6906
+ );
6907
+ }
6908
+ function escapeMarkdown(value) {
6909
+ if (value instanceof MarkdownSafeString) {
6910
+ return value.toString();
6911
+ }
6912
+ if (Array.isArray(value)) {
6913
+ return joinMarkdown(value).toString();
6914
+ }
6915
+ return String(value).replace(
6916
+ markdownEscapablesRegex,
6917
+ (character) => markdownEscapables[character]
6918
+ );
6919
+ }
6920
+ var MarkdownSafeString = class {
6921
+ constructor(strings, values) {
6922
+ this._strings = strings;
6923
+ this._values = values;
6924
+ }
6925
+ toString() {
6926
+ return this._strings.reduce((result, str, i) => {
6927
+ return result + escapeMarkdown(nn(this._values[i - 1])) + str;
6928
+ });
6929
+ }
6930
+ };
6931
+ function markdown(strings, ...values) {
6932
+ return new MarkdownSafeString(strings, values);
6933
+ }
6934
+ function toAbsoluteUrl(url) {
6935
+ if (url.startsWith("http://") || url.startsWith("https://")) {
6936
+ return url;
6937
+ } else if (url.startsWith("www.")) {
6938
+ return "https://" + url;
6939
+ }
6940
+ return;
6941
+ }
6942
+ var stringifyCommentBodyPlainElements = {
6943
+ paragraph: ({ children }) => children,
6944
+ text: ({ element }) => element.text,
6945
+ link: ({ element }) => element.url,
6946
+ mention: ({ element, user }) => {
6947
+ return `@${user?.name ?? element.id}`;
6948
+ }
6949
+ };
6950
+ var stringifyCommentBodyHtmlElements = {
6951
+ paragraph: ({ children }) => {
6952
+ return children ? html`<p>${htmlSafe(children)}</p>` : children;
6953
+ },
6954
+ text: ({ element }) => {
6955
+ let children = element.text;
6956
+ if (!children) {
6957
+ return children;
6958
+ }
6959
+ if (element.bold) {
6960
+ children = html`<strong>${children}</strong>`;
6961
+ }
6962
+ if (element.italic) {
6963
+ children = html`<em>${children}</em>`;
6964
+ }
6965
+ if (element.strikethrough) {
6966
+ children = html`<s>${children}</s>`;
6967
+ }
6968
+ if (element.code) {
6969
+ children = html`<code>${children}</code>`;
6970
+ }
6971
+ return children;
6972
+ },
6973
+ link: ({ element, href }) => {
6974
+ return html`<a href="${href}" target="_blank" rel="noopener noreferrer">${element.url}</a>`;
6975
+ },
6976
+ mention: ({ element, user }) => {
6977
+ return html`<span data-mention>@${user?.name ?? element.id}</span>`;
6978
+ }
6979
+ };
6980
+ var stringifyCommentBodyMarkdownElements = {
6981
+ paragraph: ({ children }) => {
6982
+ return children;
6983
+ },
6984
+ text: ({ element }) => {
6985
+ let children = element.text;
6986
+ if (!children) {
6987
+ return children;
6988
+ }
6989
+ if (element.bold) {
6990
+ children = markdown`**${children}**`;
6991
+ }
6992
+ if (element.italic) {
6993
+ children = markdown`_${children}_`;
6994
+ }
6995
+ if (element.strikethrough) {
6996
+ children = markdown`~~${children}~~`;
6997
+ }
6998
+ if (element.code) {
6999
+ children = markdown`\`${children}\``;
7000
+ }
7001
+ return children;
7002
+ },
7003
+ link: ({ element, href }) => {
7004
+ return markdown`[${element.url}](${href})`;
7005
+ },
7006
+ mention: ({ element, user }) => {
7007
+ return markdown`@${user?.name ?? element.id}`;
7008
+ }
7009
+ };
7010
+ async function stringifyCommentBody(body, options) {
7011
+ const format = options?.format ?? "plain";
7012
+ const separator = options?.separator ?? (format === "markdown" ? "\n\n" : "\n");
7013
+ const elements = {
7014
+ ...format === "html" ? stringifyCommentBodyHtmlElements : format === "markdown" ? stringifyCommentBodyMarkdownElements : stringifyCommentBodyPlainElements,
7015
+ ...options?.elements
7016
+ };
7017
+ const resolvedUsers = await resolveUsersInCommentBody(
7018
+ body,
7019
+ options?.resolveUsers
7020
+ );
7021
+ const blocks = body.content.flatMap((block, blockIndex) => {
7022
+ switch (block.type) {
7023
+ case "paragraph": {
7024
+ const inlines = block.children.flatMap((inline, inlineIndex) => {
7025
+ if (isCommentBodyMention(inline)) {
7026
+ return inline.id ? [
7027
+ elements.mention(
7028
+ {
7029
+ element: inline,
7030
+ user: resolvedUsers.get(inline.id)
7031
+ },
7032
+ inlineIndex
7033
+ )
7034
+ ] : [];
7035
+ }
7036
+ if (isCommentBodyLink(inline)) {
7037
+ return [
7038
+ elements.link(
7039
+ {
7040
+ element: inline,
7041
+ href: toAbsoluteUrl(inline.url) ?? inline.url
7042
+ },
7043
+ inlineIndex
7044
+ )
7045
+ ];
7046
+ }
7047
+ if (isCommentBodyText(inline)) {
7048
+ return [elements.text({ element: inline }, inlineIndex)];
7049
+ }
7050
+ return [];
7051
+ });
7052
+ return [
7053
+ elements.paragraph(
7054
+ { element: block, children: inlines.join("") },
7055
+ blockIndex
7056
+ )
7057
+ ];
7058
+ }
7059
+ default:
7060
+ return [];
7061
+ }
7062
+ });
7063
+ return blocks.join(separator);
7064
+ }
7065
+
6756
7066
  // src/index.ts
6757
7067
  detectDupes(PKG_NAME, PKG_VERSION, PKG_FORMAT);
6758
7068
  export {
@@ -6779,6 +7089,7 @@ export {
6779
7089
  detectDupes,
6780
7090
  errorIf,
6781
7091
  freeze,
7092
+ getMentionedIdsFromCommentBody,
6782
7093
  isChildCrdt,
6783
7094
  isJsonArray,
6784
7095
  isJsonObject,
@@ -6796,6 +7107,7 @@ export {
6796
7107
  raise,
6797
7108
  shallow,
6798
7109
  stringify,
7110
+ stringifyCommentBody,
6799
7111
  throwUsageError,
6800
7112
  toPlainLson,
6801
7113
  tryParseJson,