@valbuild/shared 0.62.6 → 0.63.1

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.
@@ -1,5 +1,4 @@
1
- import { VAL_EXTENSION, Internal, FILE_REF_PROP, FILE_REF_SUBTYPE_TAG, RT_IMAGE_TAG } from '@valbuild/core';
2
- import * as marked from 'marked';
1
+ import { Internal, VAL_EXTENSION, FILE_REF_PROP } from '@valbuild/core';
3
2
  import { z } from 'zod';
4
3
  import { result } from '@valbuild/core/fp';
5
4
 
@@ -34,6 +33,28 @@ function _defineProperty(obj, key, value) {
34
33
  return obj;
35
34
  }
36
35
 
36
+ function ownKeys(e, r) {
37
+ var t = Object.keys(e);
38
+ if (Object.getOwnPropertySymbols) {
39
+ var o = Object.getOwnPropertySymbols(e);
40
+ r && (o = o.filter(function (r) {
41
+ return Object.getOwnPropertyDescriptor(e, r).enumerable;
42
+ })), t.push.apply(t, o);
43
+ }
44
+ return t;
45
+ }
46
+ function _objectSpread2(e) {
47
+ for (var r = 1; r < arguments.length; r++) {
48
+ var t = null != arguments[r] ? arguments[r] : {};
49
+ r % 2 ? ownKeys(Object(t), !0).forEach(function (r) {
50
+ _defineProperty(e, r, t[r]);
51
+ }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) {
52
+ Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r));
53
+ });
54
+ }
55
+ return e;
56
+ }
57
+
37
58
  function _arrayLikeToArray(arr, len) {
38
59
  if (len == null || len > arr.length) len = arr.length;
39
60
  for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
@@ -101,22 +122,6 @@ function _createForOfIteratorHelper(o, allowArrayLike) {
101
122
  };
102
123
  }
103
124
 
104
- function _arrayWithoutHoles(arr) {
105
- if (Array.isArray(arr)) return _arrayLikeToArray(arr);
106
- }
107
-
108
- function _iterableToArray(iter) {
109
- if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
110
- }
111
-
112
- function _nonIterableSpread() {
113
- throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
114
- }
115
-
116
- function _toConsumableArray(arr) {
117
- return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread();
118
- }
119
-
120
125
  function _arrayWithHoles(arr) {
121
126
  if (Array.isArray(arr)) return arr;
122
127
  }
@@ -881,184 +886,183 @@ function filenameToMimeType(filename) {
881
886
 
882
887
  function remirrorToRichTextSource(node) {
883
888
  var files = {};
884
- var markdownIRBlocks = node.content.map(function (child) {
885
- return createBlock(child, files);
886
- });
887
- return fromIRToRichTextSource(markdownIRBlocks, files);
889
+ var blocks = [];
890
+ var i = 0;
891
+ var _iterator = _createForOfIteratorHelper(node.content),
892
+ _step;
893
+ try {
894
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
895
+ var child = _step.value;
896
+ var block = convertBlock([(i++).toString()], child, files);
897
+ blocks.push(block);
898
+ }
899
+ } catch (err) {
900
+ _iterator.e(err);
901
+ } finally {
902
+ _iterator.f();
903
+ }
904
+ return {
905
+ blocks: blocks,
906
+ files: files
907
+ };
888
908
  }
889
- function createBlock(node, files) {
909
+ function convertBlock(path, node, files) {
890
910
  if (node.type === "heading") {
891
911
  var _node$attrs, _node$content;
892
- var headingTag = "";
893
912
  var depth = ((_node$attrs = node.attrs) === null || _node$attrs === void 0 ? void 0 : _node$attrs.level) || 1;
894
913
  if (Number.isNaN(depth)) {
895
914
  throw new Error("Invalid header depth");
896
915
  }
897
- for (var i = 0; i < Number(depth); i++) {
898
- headingTag += "#";
899
- }
900
- var headingText = ["".concat(headingTag, " ")];
901
916
  return {
902
- type: "block",
903
- children: headingText.concat.apply(headingText, _toConsumableArray(((_node$content = node.content) === null || _node$content === void 0 ? void 0 : _node$content.map(function (child) {
904
- return transformLeafNode(child, files);
905
- })) || []))
906
- };
907
- } else if (node.type === "paragraph") {
908
- var _node$content2;
909
- if (!node.content || ((_node$content2 = node.content) === null || _node$content2 === void 0 ? void 0 : _node$content2.length) === 0) {
910
- return {
911
- type: "block",
912
- children: ["<br />"]
913
- };
914
- }
915
- return {
916
- type: "block",
917
- children: node.content.map(function (child) {
918
- return transformLeafNode(child, files);
919
- })
920
- };
921
- } else if (node.type === "bulletList" || node.type === "orderedList") {
922
- var _node$content3;
923
- return {
924
- type: "block",
925
- children: ((_node$content3 = node.content) === null || _node$content3 === void 0 ? void 0 : _node$content3.flatMap(function (child, i) {
926
- return formatListItemNode(getListPrefix(node), child, 0, files, i === 0);
917
+ tag: "h".concat(depth),
918
+ children: ((_node$content = node.content) === null || _node$content === void 0 ? void 0 : _node$content.map(function (child, i) {
919
+ return convertHeadingChild(path.concat("children", i.toString()), child, files);
927
920
  })) || []
928
921
  };
922
+ } else if (node.type === "paragraph") {
923
+ return convertParagraph(path, node, files);
924
+ } else if (node.type === "bulletList") {
925
+ return convertBulletList(path, node, files);
926
+ } else if (node.type === "orderedList") {
927
+ return convertOrderedList(path, node, files);
929
928
  } else {
930
929
  var exhaustiveCheck = node;
931
930
  throw new Error("Unhandled node type: ".concat("type" in exhaustiveCheck ? "exhaustiveCheck.type" : "unknown"));
932
931
  }
933
932
  }
934
- function fromIRToRichTextSource(markdownIRBlocks, files) {
935
- var templateStrings = ["\n"];
936
- var exprs = [];
937
- for (var blockIdx = 0; blockIdx < markdownIRBlocks.length; blockIdx++) {
938
- var block = markdownIRBlocks[blockIdx];
939
- var _iterator = _createForOfIteratorHelper(block.children),
940
- _step;
941
- try {
942
- for (_iterator.s(); !(_step = _iterator.n()).done;) {
943
- var child = _step.value;
944
- if (typeof child === "string") {
945
- templateStrings[templateStrings.length - 1] += child;
946
- } else {
947
- if (child._type === "file") {
948
- exprs.push(child);
949
- } else if (child._type === "link") {
950
- exprs.push(child);
951
- } else {
952
- var exhaustiveCheck = child;
953
- throw new Error("Unexpected node type: ".concat(JSON.stringify(exhaustiveCheck, null, 2)));
954
- }
955
- templateStrings.push("");
956
- }
957
- }
958
- } catch (err) {
959
- _iterator.e(err);
960
- } finally {
961
- _iterator.f();
962
- }
963
- if (blockIdx === markdownIRBlocks.length - 1) {
964
- templateStrings[templateStrings.length - 1] += "\n";
965
- } else {
966
- templateStrings[templateStrings.length - 1] += "\n\n";
967
- }
933
+ function convertHeadingChild(path, node, files) {
934
+ if (node.type === "text") {
935
+ return convertTextNode(node);
936
+ } else if (node.type === "hardBreak") {
937
+ return {
938
+ tag: "br"
939
+ };
940
+ } else if (node.type === "image") {
941
+ return convertImageNode(path, node, files);
942
+ } else {
943
+ var exhaustiveCheck = node;
944
+ throw new Error("Unexpected heading child type: ".concat(JSON.stringify(exhaustiveCheck, null, 2)));
968
945
  }
969
- return _defineProperty(_defineProperty(_defineProperty(_defineProperty({}, VAL_EXTENSION, "richtext"), "templateStrings", templateStrings), "exprs", exprs), "files", files);
970
946
  }
971
- function formatText(node) {
972
- var _node$marks;
973
- var classes = ((_node$marks = node.marks) === null || _node$marks === void 0 ? void 0 : _node$marks.flatMap(function (mark) {
974
- if (mark.type === "bold") {
975
- return ["bold"];
976
- } else if (mark.type === "italic") {
977
- return ["italic"];
978
- } else if (mark.type === "strike") {
979
- return ["line-through"];
980
- }
981
- return [];
982
- })) || [];
983
- var text = node.text.trimStart();
984
- var prefixWS = node.text.length - text.length;
985
- text = text.trimEnd();
986
- var suffixWS = node.text.length - text.length - prefixWS;
987
- if (classes.includes("bold") && classes.includes("italic")) {
988
- text = "***".concat(text, "***");
989
- } else if (classes.includes("bold")) {
990
- text = "**".concat(text, "**");
991
- } else if (classes.includes("italic")) {
992
- text = "_".concat(text, "_");
993
- }
994
- if (classes.includes("line-through")) {
995
- text = "~~".concat(text, "~~");
996
- }
997
- // TODO:
998
- // text = splitIntoChunks(text);
999
- return "".concat(" ".repeat(prefixWS)).concat(text).concat(" ".repeat(suffixWS));
947
+ function convertParagraph(path, child, files) {
948
+ var _child$content;
949
+ return {
950
+ tag: "p",
951
+ children: ((_child$content = child.content) === null || _child$content === void 0 ? void 0 : _child$content.map(function (child, i) {
952
+ if (child.type === "text") {
953
+ return convertTextNode(child);
954
+ } else if (child.type === "hardBreak") {
955
+ return {
956
+ tag: "br"
957
+ };
958
+ } else if (child.type === "image") {
959
+ return convertImageNode(path.concat("children", i.toString()), child, files);
960
+ }
961
+ var exhaustiveCheck = child;
962
+ throw new Error("Unexpected paragraph child type: ".concat(JSON.stringify(exhaustiveCheck, null, 2)));
963
+ })) || []
964
+ };
1000
965
  }
1001
- function transformLeafNode(node, files) {
966
+ function convertTextNode(node) {
1002
967
  if (node.type === "text") {
1003
- var _node$marks2;
1004
- var linkMark = (_node$marks2 = node.marks) === null || _node$marks2 === void 0 ? void 0 : _node$marks2.find(function (mark) {
968
+ var _node$marks, _node$marks2;
969
+ var styles = ((_node$marks = node.marks) === null || _node$marks === void 0 ? void 0 : _node$marks.flatMap(function (mark) {
970
+ if (mark.type !== "link") {
971
+ if (mark.type === "strike") {
972
+ return ["line-through"];
973
+ }
974
+ return [mark.type];
975
+ } else {
976
+ return [];
977
+ }
978
+ })) || [];
979
+ if ((_node$marks2 = node.marks) !== null && _node$marks2 !== void 0 && _node$marks2.some(function (mark) {
1005
980
  return mark.type === "link";
1006
- });
1007
- if ((linkMark === null || linkMark === void 0 ? void 0 : linkMark.type) === "link") {
981
+ })) {
982
+ var _node$marks$find2;
983
+ if (styles.length > 0) {
984
+ var _node$marks$find;
985
+ return {
986
+ tag: "a",
987
+ href: ((_node$marks$find = node.marks.find(function (mark) {
988
+ return mark.type === "link";
989
+ })) === null || _node$marks$find === void 0 ? void 0 : _node$marks$find.attrs.href) || "",
990
+ children: [{
991
+ tag: "span",
992
+ styles: styles,
993
+ children: [node.text]
994
+ }]
995
+ };
996
+ }
1008
997
  return {
1009
- _type: "link",
1010
- href: linkMark.attrs.href,
1011
- children: [formatText(node)]
998
+ tag: "a",
999
+ href: ((_node$marks$find2 = node.marks.find(function (mark) {
1000
+ return mark.type === "link";
1001
+ })) === null || _node$marks$find2 === void 0 ? void 0 : _node$marks$find2.attrs.href) || "",
1002
+ children: [node.text]
1012
1003
  };
1013
1004
  }
1014
- return formatText(node);
1015
- } else if (node.type === "hardBreak") {
1016
- return "<br />";
1017
- } else if (node.type === "image") {
1018
- return fromRemirrorImageNode(node, files);
1005
+ if (styles.length > 0) {
1006
+ return {
1007
+ tag: "span",
1008
+ styles: styles,
1009
+ children: [node.text]
1010
+ };
1011
+ }
1012
+ return node.text;
1019
1013
  } else {
1020
- var exhaustiveCheck = node;
1021
- throw new Error("Unexpected node type: ".concat(JSON.stringify(exhaustiveCheck)));
1014
+ var exhaustiveCheck = node.type;
1015
+ throw new Error("Unexpected node type: ".concat(exhaustiveCheck));
1022
1016
  }
1023
1017
  }
1024
- function formatListItemNode(listPrefix, node, indent, files) {
1025
- var _node$content4, _node$content5;
1026
- var isFirstTopLevelListItem = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : false;
1027
- var newLine = isFirstTopLevelListItem ? "" : "\n";
1028
- var prefix = ["".concat(newLine).concat(" ".repeat(indent)).concat(listPrefix)];
1029
- if (!(((_node$content4 = node.content) === null || _node$content4 === void 0 || (_node$content4 = _node$content4[0]) === null || _node$content4 === void 0 ? void 0 : _node$content4.type) === "bulletList" || ((_node$content5 = node.content) === null || _node$content5 === void 0 || (_node$content5 = _node$content5[0]) === null || _node$content5 === void 0 ? void 0 : _node$content5.type) === "orderedList")) {
1030
- prefix.push(" ");
1031
- }
1032
- return prefix.concat((node.content || []).flatMap(function (child, i) {
1033
- if (child.type === "bulletList" || child.type === "orderedList") {
1034
- return (child.content || []).flatMap(function (subChild) {
1035
- return formatListItemNode(getListPrefix(child), subChild, indent + 4, files);
1036
- });
1037
- } else {
1038
- return (child.content || []).flatMap(function (subChild) {
1039
- var _child$content, _child$content2;
1040
- var res = [];
1041
- if ((_child$content = child.content) !== null && _child$content !== void 0 && _child$content.length && ((_child$content2 = child.content) === null || _child$content2 === void 0 ? void 0 : _child$content2.length) > 0 && i > 0) {
1042
- res.push("<br />\n");
1043
- }
1044
- res.push(transformLeafNode(subChild, files));
1045
- return res;
1046
- });
1047
- }
1048
- }));
1018
+ function convertListItem(path, child, files) {
1019
+ var _child$content2;
1020
+ return {
1021
+ tag: "li",
1022
+ children: ((_child$content2 = child.content) === null || _child$content2 === void 0 ? void 0 : _child$content2.map(function (child, i) {
1023
+ if (child.type === "paragraph") {
1024
+ return convertParagraph(path.concat("children", i.toString()), child, files);
1025
+ } else if (child.type === "bulletList") {
1026
+ return convertBulletList(path.concat("children", i.toString()), child, files);
1027
+ } else if (child.type === "orderedList") {
1028
+ return convertOrderedList(path.concat("children", i.toString()), child, files);
1029
+ } else {
1030
+ var exhaustiveCheck = child;
1031
+ throw new Error("Unexpected list child type: ".concat(exhaustiveCheck));
1032
+ }
1033
+ })) || []
1034
+ };
1049
1035
  }
1050
- function getListPrefix(node) {
1051
- if (node.type === "bulletList") {
1052
- return "-";
1053
- } else if (node.type === "orderedList") {
1054
- return "1.";
1055
- } else {
1056
- var exhaustiveCheck = node;
1057
- throw new Error("Unhandled list node: ".concat(JSON.stringify(exhaustiveCheck)));
1058
- }
1036
+ function convertBulletList(path, node, files) {
1037
+ var _node$content2;
1038
+ return {
1039
+ tag: "ul",
1040
+ children: ((_node$content2 = node.content) === null || _node$content2 === void 0 ? void 0 : _node$content2.map(function (child, i) {
1041
+ if (child.type === "listItem") {
1042
+ return convertListItem(path.concat("children", i.toString()), child, files);
1043
+ } else {
1044
+ var exhaustiveCheck = child.type;
1045
+ throw new Error("Unexpected bullet list child type: ".concat(exhaustiveCheck));
1046
+ }
1047
+ })) || []
1048
+ };
1049
+ }
1050
+ function convertOrderedList(path, node, files) {
1051
+ var _node$content3;
1052
+ return {
1053
+ tag: "ol",
1054
+ children: ((_node$content3 = node.content) === null || _node$content3 === void 0 ? void 0 : _node$content3.map(function (child, i) {
1055
+ if (child.type === "listItem") {
1056
+ return convertListItem(path.concat("children", i.toString()), child, files);
1057
+ } else {
1058
+ var exhaustiveCheck = child.type;
1059
+ throw new Error("Unexpected ordered list child type: ".concat(exhaustiveCheck));
1060
+ }
1061
+ })) || []
1062
+ };
1059
1063
  }
1060
1064
  var textEncoder = new TextEncoder();
1061
- function fromRemirrorImageNode(node, files) {
1065
+ function convertImageNode(path, node, files) {
1062
1066
  if (node.attrs && node.attrs.src.startsWith("data:")) {
1063
1067
  var sha256 = Internal.getSHA256Hash(textEncoder.encode(node.attrs.src));
1064
1068
  var mimeType = getMimeType(node.attrs.src);
@@ -1068,22 +1072,43 @@ function fromRemirrorImageNode(node, files) {
1068
1072
  var fileExt = mimeTypeToFileExt(mimeType);
1069
1073
  var fileName = node.attrs.fileName || "".concat(sha256, ".").concat(fileExt);
1070
1074
  var filePath = "/public/".concat(fileName);
1071
- files[filePath] = node.attrs.src;
1072
- return _defineProperty(_defineProperty(_defineProperty(_defineProperty({}, VAL_EXTENSION, "file"), FILE_REF_PROP, filePath), FILE_REF_SUBTYPE_TAG, RT_IMAGE_TAG), "metadata", {
1073
- width: typeof node.attrs.width === "number" ? node.attrs.width : 0,
1074
- height: typeof node.attrs.height === "number" ? node.attrs.height : 0,
1075
- sha256: sha256 || "",
1076
- mimeType: mimeType
1077
- });
1075
+ var existingFilesEntry = files[filePath];
1076
+ var thisPath = path
1077
+ // file is added as src (see below):
1078
+ .concat("src");
1079
+ if (existingFilesEntry) {
1080
+ existingFilesEntry.patchPaths.push(thisPath);
1081
+ } else {
1082
+ files[filePath] = {
1083
+ value: node.attrs.src,
1084
+ patchPaths: [thisPath]
1085
+ };
1086
+ }
1087
+ return {
1088
+ tag: "img",
1089
+ src: _defineProperty(_defineProperty(_defineProperty({}, VAL_EXTENSION, "file"), FILE_REF_PROP, filePath), "metadata", {
1090
+ width: typeof node.attrs.width === "number" ? node.attrs.width : 0,
1091
+ height: typeof node.attrs.height === "number" ? node.attrs.height : 0,
1092
+ sha256: sha256 || "",
1093
+ mimeType: mimeType
1094
+ })
1095
+ };
1078
1096
  } else if (node.attrs) {
1079
1097
  var _sha = getParam("sha256", node.attrs.src);
1098
+ var patchId = getParam("patch_id", node.attrs.src);
1080
1099
  var noParamsSrc = node.attrs.src.split("?")[0];
1081
- return _defineProperty(_defineProperty(_defineProperty(_defineProperty({}, VAL_EXTENSION, "file"), FILE_REF_PROP, "/public".concat(node.attrs.src.split("?")[0])), FILE_REF_SUBTYPE_TAG, RT_IMAGE_TAG), "metadata", {
1082
- width: typeof node.attrs.width === "number" ? node.attrs.width : 0,
1083
- height: typeof node.attrs.height === "number" ? node.attrs.height : 0,
1084
- sha256: _sha || "",
1085
- mimeType: noParamsSrc && filenameToMimeType(noParamsSrc) || ""
1086
- });
1100
+ var tag = {
1101
+ tag: "img",
1102
+ src: _objectSpread2(_defineProperty(_defineProperty(_defineProperty({}, VAL_EXTENSION, "file"), FILE_REF_PROP, "/public".concat(node.attrs.src.split("?")[0])), "metadata", {
1103
+ width: typeof node.attrs.width === "number" ? node.attrs.width : 0,
1104
+ height: typeof node.attrs.height === "number" ? node.attrs.height : 0,
1105
+ sha256: _sha || "",
1106
+ mimeType: noParamsSrc && filenameToMimeType(noParamsSrc) || ""
1107
+ }), patchId ? {
1108
+ patch_id: patchId
1109
+ } : {})
1110
+ };
1111
+ return tag;
1087
1112
  } else {
1088
1113
  throw new Error("Invalid image node (no attrs): " + JSON.stringify(node));
1089
1114
  }
@@ -1101,188 +1126,6 @@ function getParam(param, url) {
1101
1126
  return undefined;
1102
1127
  }
1103
1128
 
1104
- var VAL_START_TAG_PREFIX = '<val value="';
1105
- var VAL_START_TAG_SUFFIX = '">';
1106
- var VAL_END_TAG = "</val>";
1107
- function parseTokens(tokens, sourceNodes, cursor) {
1108
- var insideList = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
1109
- var children = [];
1110
- function merge(token, clazz) {
1111
- var parsedTokens = parseTokens(token.tokens ? token.tokens : [], sourceNodes, 0);
1112
- children.push({
1113
- tag: "span",
1114
- classes: [clazz].concat(parsedTokens.children.flatMap(function (child) {
1115
- return typeof child === "string" ? [] : child.classes;
1116
- })),
1117
- children: parsedTokens.children.flatMap(function (child) {
1118
- return typeof child === "string" ? child : child.children;
1119
- })
1120
- });
1121
- }
1122
- var _loop = function _loop() {
1123
- var token = tokens[cursor];
1124
- if (token.type === "heading") {
1125
- children.push({
1126
- tag: "h".concat(token.depth),
1127
- children: parseTokens(token.tokens ? token.tokens : [], sourceNodes, 0).children
1128
- });
1129
- } else if (token.type === "paragraph") {
1130
- children.push({
1131
- tag: "p",
1132
- children: parseTokens(token.tokens ? token.tokens : [], sourceNodes, 0).children
1133
- });
1134
- } else if (token.type === "strong") {
1135
- merge(token, "bold");
1136
- } else if (token.type === "em") {
1137
- merge(token, "italic");
1138
- } else if (token.type === "del") {
1139
- merge(token, "line-through");
1140
- } else if (token.type === "text") {
1141
- if ("tokens" in token && Array.isArray(token.tokens)) {
1142
- children.push.apply(children, _toConsumableArray(parseTokens(token.tokens, sourceNodes, cursor, insideList).children));
1143
- } else {
1144
- if (insideList && typeof token.raw === "string") {
1145
- var lines = token.raw.split("\n");
1146
- var tags = lines.flatMap(function (line, i) {
1147
- if (i === lines.length - 1) return [line];
1148
- if (i === lines.length - 1 && line === "") return [];
1149
- if (line === "") return {
1150
- tag: "br"
1151
- };
1152
- return [line, {
1153
- tag: "br"
1154
- }];
1155
- });
1156
- children.push.apply(children, _toConsumableArray(tags));
1157
- } else {
1158
- children.push(token.raw);
1159
- }
1160
- }
1161
- } else if (token.type === "list") {
1162
- children.push({
1163
- tag: token.ordered ? "ol" : "ul",
1164
- children: parseTokens(token.items, sourceNodes, 0).children
1165
- });
1166
- } else if (token.type === "list_item") {
1167
- children.push({
1168
- tag: "li",
1169
- children: parseTokens(token.tokens ? token.tokens : [], sourceNodes, 0, true).children
1170
- });
1171
- } else if (token.type === "space") ; else if (token.type === "html") {
1172
- if (token.text === VAL_END_TAG) {
1173
- return {
1174
- v: {
1175
- children: children,
1176
- cursor: cursor
1177
- }
1178
- };
1179
- }
1180
- var suffixIndex = token.text.indexOf(VAL_START_TAG_SUFFIX);
1181
- if (token.text.startsWith(VAL_START_TAG_PREFIX) && suffixIndex > -1) {
1182
- var number = Number(token.text.slice(VAL_START_TAG_PREFIX.length, suffixIndex));
1183
- if (Number.isNaN(number)) {
1184
- throw Error("Illegal val intermediate node: ".concat(JSON.stringify(token)));
1185
- }
1186
- var _parseTokens = parseTokens(tokens.map(function (token) {
1187
- if (token.type === "link" || token.type === "list") {
1188
- return {
1189
- type: "text",
1190
- raw: token.raw,
1191
- text: token.raw
1192
- };
1193
- }
1194
- return token;
1195
- }), sourceNodes, cursor + 1),
1196
- subChildren = _parseTokens.children,
1197
- subCursor = _parseTokens.cursor;
1198
- var sourceNode = sourceNodes[number];
1199
- if (sourceNode._type === "link") {
1200
- children.push({
1201
- tag: "a",
1202
- href: sourceNode.href,
1203
- children: subChildren
1204
- });
1205
- } else if (sourceNode._type === "file") {
1206
- var _sourceNode$metadata, _sourceNode$metadata2, _sourceNode$metadata3;
1207
- children.push({
1208
- tag: "img",
1209
- src: Internal.convertFileSource(sourceNode).url,
1210
- width: (_sourceNode$metadata = sourceNode.metadata) === null || _sourceNode$metadata === void 0 ? void 0 : _sourceNode$metadata.width,
1211
- height: (_sourceNode$metadata2 = sourceNode.metadata) === null || _sourceNode$metadata2 === void 0 ? void 0 : _sourceNode$metadata2.height,
1212
- mimeType: (_sourceNode$metadata3 = sourceNode.metadata) === null || _sourceNode$metadata3 === void 0 ? void 0 : _sourceNode$metadata3.mimeType
1213
- });
1214
- }
1215
- cursor = subCursor;
1216
- }
1217
- var br_html_regex = /<br\s*\/?>/gi; // matches <br>, <br/>, <br />; case insensitive
1218
- if (token.text.trim().match(br_html_regex)) {
1219
- var _tokens;
1220
- children.push({
1221
- tag: "br"
1222
- });
1223
- if (((_tokens = tokens[cursor + 1]) === null || _tokens === void 0 ? void 0 : _tokens.raw.trim()) === "") {
1224
- // if next token is a new line or white-spaces, skip it
1225
- // this typically means we have a <br> AND a new line, which, semantically, is just a <br>
1226
- cursor++;
1227
- }
1228
- }
1229
- } else if (token.type === "link") {
1230
- if (token.raw === token.href) {
1231
- // avoid auto-linking (provided by github flavoured markdown, but we want strikethrough so keep it enabled)
1232
- children.push(token.raw);
1233
- } else {
1234
- children.push({
1235
- tag: "a",
1236
- href: token.href,
1237
- children: parseTokens(token.tokens ? token.tokens : [], sourceNodes, 0).children
1238
- });
1239
- }
1240
- } else if (token.type === "br") {
1241
- children.push({
1242
- tag: "br"
1243
- });
1244
- } else {
1245
- console.error("Could not parse markdown: unsupported token type: ".concat(token.type, ". Found: ").concat(token.raw));
1246
- }
1247
- cursor++;
1248
- },
1249
- _ret;
1250
- while (cursor < tokens.length) {
1251
- _ret = _loop();
1252
- if (_ret) return _ret.v;
1253
- }
1254
- return {
1255
- children: children,
1256
- cursor: cursor
1257
- };
1258
- }
1259
- function parseRichTextSource(_ref) {
1260
- var templateStrings = _ref.templateStrings,
1261
- nodes = _ref.exprs;
1262
- // TODO: validate that templateStrings does not contain VAL_NODE_PREFIX
1263
- var inputText = templateStrings.flatMap(function (templateString, i) {
1264
- var node = nodes[i];
1265
- if (node) {
1266
- if (node[VAL_EXTENSION] === "link") {
1267
- return templateString.concat("".concat(VAL_START_TAG_PREFIX).concat(i).concat(VAL_START_TAG_SUFFIX).concat(node.children[0]).concat(VAL_END_TAG));
1268
- } else {
1269
- return templateString.concat("".concat(VAL_START_TAG_PREFIX).concat(i).concat(VAL_START_TAG_SUFFIX).concat(VAL_END_TAG));
1270
- }
1271
- }
1272
- return templateString;
1273
- }).join("");
1274
- var tokenList = marked.lexer(inputText, {
1275
- gfm: true
1276
- });
1277
- var _parseTokens2 = parseTokens(tokenList, nodes, 0),
1278
- children = _parseTokens2.children,
1279
- cursor = _parseTokens2.cursor;
1280
- if (cursor !== tokenList.length) {
1281
- throw Error("Unexpectedly terminated markdown parsing. Possible reason: unclosed html tag?");
1282
- }
1283
- return _defineProperty(_defineProperty({}, VAL_EXTENSION, "richtext"), "children", children);
1284
- }
1285
-
1286
1129
  var RemirrorTextMark = z.object({
1287
1130
  type: z.union([z.literal("bold"), z.literal("strike"), z.literal("italic")])
1288
1131
  });
@@ -1326,7 +1169,7 @@ var RemirrorHeading = z.intersection(z.object({
1326
1169
  attrs: z.object({
1327
1170
  level: z.number()
1328
1171
  }),
1329
- content: z.array(z.union([RemirrorText, RemirrorBr]))
1172
+ content: z.array(z.union([RemirrorText, RemirrorImage, RemirrorBr]))
1330
1173
  }).partial());
1331
1174
  var RemirrorBulletList = z.lazy(function () {
1332
1175
  return z.intersection(z.object({
@@ -1346,45 +1189,45 @@ var RemirrorListItem = z.lazy(function () {
1346
1189
  return z.intersection(z.object({
1347
1190
  type: z.literal("listItem")
1348
1191
  }), z.object({
1192
+ attrs: z.object({
1193
+ closed: z["boolean"](),
1194
+ nested: z["boolean"]()
1195
+ }).partial().optional(),
1349
1196
  content: z.array(z.union([RemirrorParagraph, RemirrorBulletList, RemirrorOrderedList]))
1350
1197
  }).partial());
1351
1198
  });
1352
- var RemirrorParagraph = z.intersection(z.object({
1353
- type: z.literal("paragraph")
1354
- }), z.object({
1355
- content: z.array(z.union([RemirrorText, RemirrorImage, RemirrorBr]))
1356
- }).partial());
1199
+ var RemirrorParagraph = z.lazy(function () {
1200
+ return z.intersection(z.object({
1201
+ type: z.literal("paragraph")
1202
+ }), z.object({
1203
+ content: z.array(z.union([RemirrorText, RemirrorImage, RemirrorBr]))
1204
+ }).partial());
1205
+ });
1357
1206
  var RemirrorJSON = z.object({
1358
1207
  type: z.literal("doc"),
1359
1208
  content: z.array(z.union([RemirrorParagraph, RemirrorHeading, RemirrorBulletList, RemirrorOrderedList]))
1360
1209
  });
1361
1210
 
1362
- function ownKeys(e, r) {
1363
- var t = Object.keys(e);
1364
- if (Object.getOwnPropertySymbols) {
1365
- var o = Object.getOwnPropertySymbols(e);
1366
- r && (o = o.filter(function (r) {
1367
- return Object.getOwnPropertyDescriptor(e, r).enumerable;
1368
- })), t.push.apply(t, o);
1369
- }
1370
- return t;
1211
+ function _arrayWithoutHoles(arr) {
1212
+ if (Array.isArray(arr)) return _arrayLikeToArray(arr);
1371
1213
  }
1372
- function _objectSpread2(e) {
1373
- for (var r = 1; r < arguments.length; r++) {
1374
- var t = null != arguments[r] ? arguments[r] : {};
1375
- r % 2 ? ownKeys(Object(t), !0).forEach(function (r) {
1376
- _defineProperty(e, r, t[r]);
1377
- }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) {
1378
- Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r));
1379
- });
1380
- }
1381
- return e;
1214
+
1215
+ function _iterableToArray(iter) {
1216
+ if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
1217
+ }
1218
+
1219
+ function _nonIterableSpread() {
1220
+ throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
1221
+ }
1222
+
1223
+ function _toConsumableArray(arr) {
1224
+ return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread();
1382
1225
  }
1383
1226
 
1384
1227
  function richTextToRemirror(richtext) {
1385
1228
  return {
1386
1229
  type: "doc",
1387
- content: richtext.children.map(function (child) {
1230
+ content: richtext.map(function (child) {
1388
1231
  return toRemirrorNode(child);
1389
1232
  })
1390
1233
  };
@@ -1409,8 +1252,6 @@ function toRemirrorNode(child) {
1409
1252
  return convertUlToRemirror(child);
1410
1253
  case "ol":
1411
1254
  return convertOlToRemirror(child);
1412
- case "br":
1413
- return convertParagraphToRemirror([child]);
1414
1255
  default:
1415
1256
  {
1416
1257
  var _exhaustiveCheck = child;
@@ -1424,7 +1265,7 @@ function convertHeadingToRemirror(level, children) {
1424
1265
  attrs: {
1425
1266
  level: level
1426
1267
  },
1427
- content: children.flatMap(convertStringOrSpanNodeToRemirror)
1268
+ content: children.flatMap(convertInlineToRemirror)
1428
1269
  };
1429
1270
  }
1430
1271
  function convertStringToRemirror(child) {
@@ -1433,14 +1274,23 @@ function convertStringToRemirror(child) {
1433
1274
  text: child
1434
1275
  };
1435
1276
  }
1436
- function convertStringOrSpanNodeToRemirror(child) {
1277
+ function convertInlineToRemirror(child) {
1437
1278
  if (typeof child === "string") {
1438
1279
  return [convertStringToRemirror(child)];
1439
1280
  }
1281
+ if (child.tag === "img") {
1282
+ return [convertImageNodeToRemirror(child)];
1283
+ }
1284
+ if (child.tag === "br") {
1285
+ return [createRemirrorBr()];
1286
+ }
1287
+ if (child.tag === "a") {
1288
+ return convertLinkNodeToRemirror(child);
1289
+ }
1440
1290
  return convertSpanNodeToRemirror(child);
1441
1291
  }
1442
1292
  function convertSpanNodeToRemirror(spanNode) {
1443
- if (spanNode.classes.length === 0 && spanNode.children.length === 1) {
1293
+ if (spanNode.styles.length === 0 && spanNode.children.length === 1) {
1444
1294
  if (typeof spanNode.children[0] === "string") {
1445
1295
  return [convertStringToRemirror(spanNode.children[0])];
1446
1296
  } else {
@@ -1448,12 +1298,13 @@ function convertSpanNodeToRemirror(spanNode) {
1448
1298
  throw Error("Unexpected amount of children in span node: " + JSON.stringify(spanNode, null, 2));
1449
1299
  }
1450
1300
  }
1301
+ var uniqueStyles = Array.from(new Set(spanNode.styles));
1451
1302
  return spanNode.children.map(function (child) {
1452
1303
  return {
1453
1304
  type: "text",
1454
1305
  text: child,
1455
- marks: spanNode.classes.map(function (className) {
1456
- switch (className) {
1306
+ marks: uniqueStyles.map(function (style) {
1307
+ switch (style) {
1457
1308
  case "bold":
1458
1309
  return {
1459
1310
  type: "bold"
@@ -1468,7 +1319,7 @@ function convertSpanNodeToRemirror(spanNode) {
1468
1319
  };
1469
1320
  default:
1470
1321
  {
1471
- var _exhaustiveCheck = className;
1322
+ var _exhaustiveCheck = style;
1472
1323
  throw Error("Unexpected span class: " + _exhaustiveCheck);
1473
1324
  }
1474
1325
  }
@@ -1508,9 +1359,12 @@ function createRemirrorBr() {
1508
1359
  };
1509
1360
  }
1510
1361
  function convertLinkNodeToRemirror(linkNode) {
1511
- return linkNode.children.flatMap(convertStringOrSpanNodeToRemirror).map(function (remirrorText) {
1512
- return _objectSpread2(_objectSpread2({}, remirrorText), {}, {
1513
- marks: (remirrorText.marks || []).concat({
1362
+ return linkNode.children.flatMap(convertInlineToRemirror).map(function (remirrorNode) {
1363
+ if (remirrorNode.type !== "text") {
1364
+ return remirrorNode;
1365
+ }
1366
+ return _objectSpread2(_objectSpread2({}, remirrorNode), {}, {
1367
+ marks: (remirrorNode.marks || []).concat({
1514
1368
  type: "link",
1515
1369
  attrs: {
1516
1370
  href: linkNode.href,
@@ -1522,12 +1376,18 @@ function convertLinkNodeToRemirror(linkNode) {
1522
1376
  });
1523
1377
  }
1524
1378
  function convertImageNodeToRemirror(imageNode) {
1379
+ var _fileSource$metadata, _fileSource$metadata2;
1380
+ var fileSource = imageNode.src;
1381
+ if (!(VAL_EXTENSION in fileSource) || fileSource[VAL_EXTENSION] !== "file") {
1382
+ throw Error("Expected file source in image node");
1383
+ }
1384
+ var fileVal = Internal.convertFileSource(fileSource);
1525
1385
  return {
1526
1386
  type: "image",
1527
1387
  attrs: {
1528
- height: imageNode.height,
1529
- width: imageNode.width,
1530
- src: imageNode.src // at time of writing we are not sure if src as href or data url works, also: how to keep mimeType etc?
1388
+ height: (_fileSource$metadata = fileSource.metadata) === null || _fileSource$metadata === void 0 ? void 0 : _fileSource$metadata.height,
1389
+ width: (_fileSource$metadata2 = fileSource.metadata) === null || _fileSource$metadata2 === void 0 ? void 0 : _fileSource$metadata2.width,
1390
+ src: fileVal.url
1531
1391
  }
1532
1392
  };
1533
1393
  }
@@ -1569,24 +1429,6 @@ function convertListItemToRemirrorParagraph(rtChildren) {
1569
1429
  lastChildContent.push(convertStringToRemirror(child));
1570
1430
  } else {
1571
1431
  switch (child.tag) {
1572
- case "a":
1573
- {
1574
- lastChildContent.push.apply(lastChildContent, _toConsumableArray(convertLinkNodeToRemirror(child)));
1575
- break;
1576
- }
1577
- case "br":
1578
- {
1579
- children.push({
1580
- type: "paragraph",
1581
- content: [] // create new paragraph
1582
- });
1583
- break;
1584
- }
1585
- case "span":
1586
- {
1587
- lastChildContent.push.apply(lastChildContent, _toConsumableArray(convertSpanNodeToRemirror(child)));
1588
- break;
1589
- }
1590
1432
  case "ol":
1591
1433
  {
1592
1434
  children.push(convertOlToRemirror(child));
@@ -1597,6 +1439,20 @@ function convertListItemToRemirrorParagraph(rtChildren) {
1597
1439
  children.push(convertUlToRemirror(child));
1598
1440
  break;
1599
1441
  }
1442
+ case "p":
1443
+ {
1444
+ var newChild = convertParagraphToRemirror(child.children);
1445
+ if (lastChild.type === "paragraph" && newChild.type === "paragraph") {
1446
+ if (newChild.content) {
1447
+ lastChildContent.push.apply(lastChildContent, _toConsumableArray(newChild.content));
1448
+ } else {
1449
+ // no content - skip
1450
+ }
1451
+ } else {
1452
+ children.push(newChild);
1453
+ }
1454
+ break;
1455
+ }
1600
1456
  default:
1601
1457
  {
1602
1458
  var _exhaustiveCheck = child;
@@ -2014,10 +1870,10 @@ var ValStore = /*#__PURE__*/function () {
2014
1870
  this.schema = {};
2015
1871
  }
2016
1872
  _createClass(ValStore, [{
2017
- key: "reset",
1873
+ key: "reloadPaths",
2018
1874
  value: function () {
2019
- var _reset = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
2020
- var patches, allPatches, data, _i, _Object$keys, pathS, path, _i2, _Array$from, _Array$from$_i, subscriberId, subscriberModules;
1875
+ var _reloadPaths = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(paths) {
1876
+ var patches, filteredPatches, data, _i, _Object$keys, pathS, path, _i2, _Array$from, _Array$from$_i, subscriberId, subscriberModules;
2021
1877
  return _regeneratorRuntime().wrap(function _callee$(_context) {
2022
1878
  while (1) switch (_context.prev = _context.next) {
2023
1879
  case 0:
@@ -2032,14 +1888,21 @@ var ValStore = /*#__PURE__*/function () {
2032
1888
  console.error("Val: failed to get patches", patches.error);
2033
1889
  return _context.abrupt("return");
2034
1890
  case 6:
2035
- allPatches = Object.values(patches.value).flatMap(function (mp) {
2036
- return mp.map(function (p) {
2037
- return p.patch_id;
2038
- });
1891
+ filteredPatches = Object.entries(patches.value).flatMap(function (_ref) {
1892
+ var _ref2 = _slicedToArray(_ref, 2),
1893
+ patchModuleFilePath = _ref2[0],
1894
+ mp = _ref2[1];
1895
+ if (paths.includes(patchModuleFilePath)) {
1896
+ return mp.map(function (p) {
1897
+ return p.patch_id;
1898
+ });
1899
+ } else {
1900
+ return [];
1901
+ }
2039
1902
  });
2040
1903
  _context.next = 9;
2041
1904
  return this.api.putTree({
2042
- patchIds: allPatches
1905
+ patchIds: filteredPatches
2043
1906
  });
2044
1907
  case 9:
2045
1908
  data = _context.sent;
@@ -2061,7 +1924,7 @@ var ValStore = /*#__PURE__*/function () {
2061
1924
  }
2062
1925
  }
2063
1926
  } else {
2064
- console.error("Val: failed to reset", data.error);
1927
+ console.error("Val: failed to reload paths", paths, data.error);
2065
1928
  }
2066
1929
  case 13:
2067
1930
  case "end":
@@ -2069,6 +1932,67 @@ var ValStore = /*#__PURE__*/function () {
2069
1932
  }
2070
1933
  }, _callee, this);
2071
1934
  }));
1935
+ function reloadPaths(_x) {
1936
+ return _reloadPaths.apply(this, arguments);
1937
+ }
1938
+ return reloadPaths;
1939
+ }()
1940
+ }, {
1941
+ key: "reset",
1942
+ value: function () {
1943
+ var _reset = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2() {
1944
+ var patches, allPatches, data, _i3, _Object$keys2, pathS, path, _i4, _Array$from2, _Array$from2$_i, subscriberId, subscriberModules;
1945
+ return _regeneratorRuntime().wrap(function _callee2$(_context2) {
1946
+ while (1) switch (_context2.prev = _context2.next) {
1947
+ case 0:
1948
+ _context2.next = 2;
1949
+ return this.api.getPatches();
1950
+ case 2:
1951
+ patches = _context2.sent;
1952
+ if (!result.isErr(patches)) {
1953
+ _context2.next = 6;
1954
+ break;
1955
+ }
1956
+ console.error("Val: failed to get patches", patches.error);
1957
+ return _context2.abrupt("return");
1958
+ case 6:
1959
+ allPatches = Object.values(patches.value).flatMap(function (mp) {
1960
+ return mp.map(function (p) {
1961
+ return p.patch_id;
1962
+ });
1963
+ });
1964
+ _context2.next = 9;
1965
+ return this.api.putTree({
1966
+ patchIds: allPatches
1967
+ });
1968
+ case 9:
1969
+ data = _context2.sent;
1970
+ _context2.next = 12;
1971
+ return this.initialize();
1972
+ case 12:
1973
+ if (result.isOk(data)) {
1974
+ for (_i3 = 0, _Object$keys2 = Object.keys(data.value.modules); _i3 < _Object$keys2.length; _i3++) {
1975
+ pathS = _Object$keys2[_i3];
1976
+ path = pathS;
1977
+ this.drafts[path] = data.value.modules[path].source;
1978
+ this.emitEvent(path, this.drafts[path]);
1979
+ for (_i4 = 0, _Array$from2 = Array.from(this.subscribers.entries()); _i4 < _Array$from2.length; _i4++) {
1980
+ _Array$from2$_i = _slicedToArray(_Array$from2[_i4], 2), subscriberId = _Array$from2$_i[0], subscriberModules = _Array$from2$_i[1];
1981
+ if (subscriberModules[path]) {
1982
+ this.subscribers.set(subscriberId, _objectSpread2(_objectSpread2({}, subscriberModules), {}, _defineProperty({}, path, this.drafts[path])));
1983
+ this.emitChange(subscriberId);
1984
+ }
1985
+ }
1986
+ }
1987
+ } else {
1988
+ console.error("Val: failed to reset", data.error);
1989
+ }
1990
+ case 13:
1991
+ case "end":
1992
+ return _context2.stop();
1993
+ }
1994
+ }, _callee2, this);
1995
+ }));
2072
1996
  function reset() {
2073
1997
  return _reset.apply(this, arguments);
2074
1998
  }
@@ -2077,37 +2001,37 @@ var ValStore = /*#__PURE__*/function () {
2077
2001
  }, {
2078
2002
  key: "getModule",
2079
2003
  value: function () {
2080
- var _getModule = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(path) {
2004
+ var _getModule = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3(path) {
2081
2005
  var refetch,
2082
2006
  data,
2083
2007
  fetchedSource,
2084
2008
  schema,
2085
- _args2 = arguments;
2086
- return _regeneratorRuntime().wrap(function _callee2$(_context2) {
2087
- while (1) switch (_context2.prev = _context2.next) {
2009
+ _args3 = arguments;
2010
+ return _regeneratorRuntime().wrap(function _callee3$(_context3) {
2011
+ while (1) switch (_context3.prev = _context3.next) {
2088
2012
  case 0:
2089
- refetch = _args2.length > 1 && _args2[1] !== undefined ? _args2[1] : false;
2013
+ refetch = _args3.length > 1 && _args3[1] !== undefined ? _args3[1] : false;
2090
2014
  if (!(!refetch && this.drafts[path] && this.schema[path])) {
2091
- _context2.next = 3;
2015
+ _context3.next = 3;
2092
2016
  break;
2093
2017
  }
2094
- return _context2.abrupt("return", result.ok({
2018
+ return _context3.abrupt("return", result.ok({
2095
2019
  source: this.drafts[path],
2096
2020
  schema: this.schema[path]
2097
2021
  }));
2098
2022
  case 3:
2099
- _context2.next = 5;
2023
+ _context3.next = 5;
2100
2024
  return this.api.putTree({
2101
2025
  treePath: path
2102
2026
  });
2103
2027
  case 5:
2104
- data = _context2.sent;
2028
+ data = _context3.sent;
2105
2029
  if (!result.isOk(data)) {
2106
- _context2.next = 26;
2030
+ _context3.next = 26;
2107
2031
  break;
2108
2032
  }
2109
2033
  if (data.value.modules[path]) {
2110
- _context2.next = 10;
2034
+ _context3.next = 10;
2111
2035
  break;
2112
2036
  }
2113
2037
  console.error("Val: could not find the module", {
@@ -2115,65 +2039,65 @@ var ValStore = /*#__PURE__*/function () {
2115
2039
  moduleId: path,
2116
2040
  data: data
2117
2041
  });
2118
- return _context2.abrupt("return", result.err({
2042
+ return _context3.abrupt("return", result.err({
2119
2043
  message: "Could not fetch data.\nCould not find the module:\n" + path + "\n\nVerify that the val.modules file includes this module."
2120
2044
  }));
2121
2045
  case 10:
2122
2046
  fetchedSource = data.value.modules[path].source;
2123
2047
  schema = this.schema[path];
2124
2048
  if (this.schema[path]) {
2125
- _context2.next = 17;
2049
+ _context3.next = 17;
2126
2050
  break;
2127
2051
  }
2128
- _context2.next = 15;
2052
+ _context3.next = 15;
2129
2053
  return this.initialize();
2130
2054
  case 15:
2131
2055
  if (this.schema[path]) {
2132
- _context2.next = 17;
2056
+ _context3.next = 17;
2133
2057
  break;
2134
2058
  }
2135
- return _context2.abrupt("return", result.err({
2059
+ return _context3.abrupt("return", result.err({
2136
2060
  message: "Path not found in schema. Verify that the module exists."
2137
2061
  }));
2138
2062
  case 17:
2139
2063
  if (!(fetchedSource !== undefined)) {
2140
- _context2.next = 22;
2064
+ _context3.next = 22;
2141
2065
  break;
2142
2066
  }
2143
2067
  this.drafts[path] = fetchedSource;
2144
- return _context2.abrupt("return", result.ok({
2068
+ return _context3.abrupt("return", result.ok({
2145
2069
  source: fetchedSource,
2146
2070
  schema: schema
2147
2071
  }));
2148
2072
  case 22:
2149
2073
  console.error("Val: could not find the module source");
2150
- return _context2.abrupt("return", result.err({
2074
+ return _context3.abrupt("return", result.err({
2151
2075
  message: "Could not fetch data. Verify that the module exists."
2152
2076
  }));
2153
2077
  case 24:
2154
- _context2.next = 33;
2078
+ _context3.next = 33;
2155
2079
  break;
2156
2080
  case 26:
2157
2081
  if (!(data.error.statusCode === 504)) {
2158
- _context2.next = 31;
2082
+ _context3.next = 31;
2159
2083
  break;
2160
2084
  }
2161
2085
  console.error("Val: timeout", data.error);
2162
- return _context2.abrupt("return", result.err({
2086
+ return _context3.abrupt("return", result.err({
2163
2087
  message: "Timed out while fetching data. Try again later."
2164
2088
  }));
2165
2089
  case 31:
2166
2090
  console.error("Val: failed to get module", data.error);
2167
- return _context2.abrupt("return", result.err({
2091
+ return _context3.abrupt("return", result.err({
2168
2092
  message: "Could not fetch data. Verify that Val is correctly configured."
2169
2093
  }));
2170
2094
  case 33:
2171
2095
  case "end":
2172
- return _context2.stop();
2096
+ return _context3.stop();
2173
2097
  }
2174
- }, _callee2, this);
2098
+ }, _callee3, this);
2175
2099
  }));
2176
- function getModule(_x) {
2100
+ function getModule(_x2) {
2177
2101
  return _getModule.apply(this, arguments);
2178
2102
  }
2179
2103
  return getModule;
@@ -2181,12 +2105,12 @@ var ValStore = /*#__PURE__*/function () {
2181
2105
  }, {
2182
2106
  key: "applyPatch",
2183
2107
  value: function () {
2184
- var _applyPatch = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3(path, patchIds, patch) {
2185
- var data, fetchedSource, _data$value$modules$p, _i3, _Array$from2, _Array$from2$_i, subscriberId, subscriberModules;
2186
- return _regeneratorRuntime().wrap(function _callee3$(_context3) {
2187
- while (1) switch (_context3.prev = _context3.next) {
2108
+ var _applyPatch = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee4(path, patchIds, patch) {
2109
+ var data, fetchedSource, _data$value$modules$p, _i5, _Array$from3, _Array$from3$_i, subscriberId, subscriberModules;
2110
+ return _regeneratorRuntime().wrap(function _callee4$(_context4) {
2111
+ while (1) switch (_context4.prev = _context4.next) {
2188
2112
  case 0:
2189
- _context3.next = 2;
2113
+ _context4.next = 2;
2190
2114
  return this.api.putTree({
2191
2115
  treePath: path,
2192
2116
  patchIds: patchIds,
@@ -2196,48 +2120,48 @@ var ValStore = /*#__PURE__*/function () {
2196
2120
  }
2197
2121
  });
2198
2122
  case 2:
2199
- data = _context3.sent;
2123
+ data = _context4.sent;
2200
2124
  if (!result.isOk(data)) {
2201
- _context3.next = 16;
2125
+ _context4.next = 16;
2202
2126
  break;
2203
2127
  }
2204
2128
  fetchedSource = data.value.modules[path].source;
2205
2129
  if (!(fetchedSource !== undefined)) {
2206
- _context3.next = 12;
2130
+ _context4.next = 12;
2207
2131
  break;
2208
2132
  }
2209
2133
  this.drafts[path] = fetchedSource;
2210
2134
  this.emitEvent(path, fetchedSource);
2211
- for (_i3 = 0, _Array$from2 = Array.from(this.subscribers.entries()); _i3 < _Array$from2.length; _i3++) {
2212
- _Array$from2$_i = _slicedToArray(_Array$from2[_i3], 2), subscriberId = _Array$from2$_i[0], subscriberModules = _Array$from2$_i[1];
2135
+ for (_i5 = 0, _Array$from3 = Array.from(this.subscribers.entries()); _i5 < _Array$from3.length; _i5++) {
2136
+ _Array$from3$_i = _slicedToArray(_Array$from3[_i5], 2), subscriberId = _Array$from3$_i[0], subscriberModules = _Array$from3$_i[1];
2213
2137
  if (subscriberModules[path]) {
2214
2138
  this.subscribers.set(subscriberId, _objectSpread2(_objectSpread2({}, subscriberModules), {}, _defineProperty({}, path, this.drafts[path])));
2215
2139
  this.emitChange(subscriberId);
2216
2140
  }
2217
2141
  }
2218
- return _context3.abrupt("return", result.ok(_defineProperty({}, path, {
2142
+ return _context4.abrupt("return", result.ok(_defineProperty({}, path, {
2219
2143
  patchIds: ((_data$value$modules$p = data.value.modules[path].patches) === null || _data$value$modules$p === void 0 ? void 0 : _data$value$modules$p.applied) || []
2220
2144
  })));
2221
2145
  case 12:
2222
2146
  console.error("Val: could not patch");
2223
- return _context3.abrupt("return", result.err({
2147
+ return _context4.abrupt("return", result.err({
2224
2148
  message: "Val: could not fetch data. Verify that the module exists."
2225
2149
  }));
2226
2150
  case 14:
2227
- _context3.next = 18;
2151
+ _context4.next = 18;
2228
2152
  break;
2229
2153
  case 16:
2230
2154
  console.error("Val: failed to get module", data.error);
2231
- return _context3.abrupt("return", result.err({
2155
+ return _context4.abrupt("return", result.err({
2232
2156
  message: "Val: could not fetch data. Verify that Val is correctly configured."
2233
2157
  }));
2234
2158
  case 18:
2235
2159
  case "end":
2236
- return _context3.stop();
2160
+ return _context4.stop();
2237
2161
  }
2238
- }, _callee3, this);
2162
+ }, _callee4, this);
2239
2163
  }));
2240
- function applyPatch(_x2, _x3, _x4) {
2164
+ function applyPatch(_x3, _x4, _x5) {
2241
2165
  return _applyPatch.apply(this, arguments);
2242
2166
  }
2243
2167
  return applyPatch;
@@ -2257,29 +2181,29 @@ var ValStore = /*#__PURE__*/function () {
2257
2181
  }, {
2258
2182
  key: "initialize",
2259
2183
  value: function () {
2260
- var _initialize = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee4() {
2261
- var data, paths, _i4, _arr, moduleId, schema, msg;
2262
- return _regeneratorRuntime().wrap(function _callee4$(_context4) {
2263
- while (1) switch (_context4.prev = _context4.next) {
2184
+ var _initialize = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee5() {
2185
+ var data, paths, _i6, _arr, moduleId, schema, msg;
2186
+ return _regeneratorRuntime().wrap(function _callee5$(_context5) {
2187
+ while (1) switch (_context5.prev = _context5.next) {
2264
2188
  case 0:
2265
- _context4.next = 2;
2189
+ _context5.next = 2;
2266
2190
  return this.api.getSchema({});
2267
2191
  case 2:
2268
- data = _context4.sent;
2192
+ data = _context5.sent;
2269
2193
  if (!result.isOk(data)) {
2270
- _context4.next = 9;
2194
+ _context5.next = 9;
2271
2195
  break;
2272
2196
  }
2273
2197
  paths = [];
2274
- for (_i4 = 0, _arr = Object.keys(data.value.schemas); _i4 < _arr.length; _i4++) {
2275
- moduleId = _arr[_i4];
2198
+ for (_i6 = 0, _arr = Object.keys(data.value.schemas); _i6 < _arr.length; _i6++) {
2199
+ moduleId = _arr[_i6];
2276
2200
  schema = data.value.schemas[moduleId];
2277
2201
  if (schema) {
2278
2202
  paths.push(moduleId);
2279
2203
  this.schema[moduleId] = schema;
2280
2204
  }
2281
2205
  }
2282
- return _context4.abrupt("return", result.ok(paths));
2206
+ return _context5.abrupt("return", result.ok(paths));
2283
2207
  case 9:
2284
2208
  msg = "Failed to fetch content. ";
2285
2209
  if (data.error.statusCode === 401) {
@@ -2287,7 +2211,7 @@ var ValStore = /*#__PURE__*/function () {
2287
2211
  } else {
2288
2212
  msg += "Get a developer to verify that Val is correctly setup.";
2289
2213
  }
2290
- return _context4.abrupt("return", result.err({
2214
+ return _context5.abrupt("return", result.err({
2291
2215
  message: msg,
2292
2216
  details: {
2293
2217
  fetchError: data.error
@@ -2295,9 +2219,9 @@ var ValStore = /*#__PURE__*/function () {
2295
2219
  }));
2296
2220
  case 12:
2297
2221
  case "end":
2298
- return _context4.stop();
2222
+ return _context5.stop();
2299
2223
  }
2300
- }, _callee4, this);
2224
+ }, _callee5, this);
2301
2225
  }));
2302
2226
  function initialize() {
2303
2227
  return _initialize.apply(this, arguments);
@@ -2327,4 +2251,4 @@ function createSubscriberId(paths) {
2327
2251
  return paths.slice().sort().join("&");
2328
2252
  }
2329
2253
 
2330
- export { EXT_TO_MIME_TYPES, MIME_TYPES_TO_EXT, RemirrorBr, RemirrorBulletList, RemirrorHeading, RemirrorImage, RemirrorJSON, RemirrorLinkMark, RemirrorListItem, RemirrorOrderedList, RemirrorParagraph, RemirrorText, RemirrorTextMark, VAL_ENABLE_COOKIE_NAME, VAL_SESSION_COOKIE, VAL_STATE_COOKIE, ValStore, filenameToMimeType, getMimeType, mimeTypeToFileExt, parseRichTextSource, remirrorToRichTextSource, richTextToRemirror };
2254
+ export { EXT_TO_MIME_TYPES, MIME_TYPES_TO_EXT, RemirrorBr, RemirrorBulletList, RemirrorHeading, RemirrorImage, RemirrorJSON, RemirrorLinkMark, RemirrorListItem, RemirrorOrderedList, RemirrorParagraph, RemirrorText, RemirrorTextMark, VAL_ENABLE_COOKIE_NAME, VAL_SESSION_COOKIE, VAL_STATE_COOKIE, ValStore, filenameToMimeType, getMimeType, mimeTypeToFileExt, remirrorToRichTextSource, richTextToRemirror };