@portabletext/block-tools 1.0.1 → 1.0.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.
@@ -1,8 +1,10 @@
1
- import flatten from "lodash/flatten.js";
2
- import { isBlockSchemaType, isBlockChildrenObjectField, isObjectSchemaType, isBlockStyleObjectField, isBlockListObjectField, isTitledListValue, isPortableTextTextBlock, isPortableTextSpan } from "@sanity/types";
3
- import isEqual from "lodash/isEqual.js";
4
- import uniq from "lodash/uniq.js";
5
- import getRandomValues from "get-random-values-esm";
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: !0 });
3
+ var flatten = require("lodash/flatten.js"), types = require("@sanity/types"), isEqual = require("lodash/isEqual.js"), uniq = require("lodash/uniq.js"), getRandomValues = require("get-random-values-esm");
4
+ function _interopDefaultCompat(e) {
5
+ return e && typeof e == "object" && "default" in e ? e : { default: e };
6
+ }
7
+ var flatten__default = /* @__PURE__ */ _interopDefaultCompat(flatten), isEqual__default = /* @__PURE__ */ _interopDefaultCompat(isEqual), uniq__default = /* @__PURE__ */ _interopDefaultCompat(uniq), getRandomValues__default = /* @__PURE__ */ _interopDefaultCompat(getRandomValues);
6
8
  function findBlockType(type) {
7
9
  return type.type ? findBlockType(type.type) : type.name === "block";
8
10
  }
@@ -90,19 +92,19 @@ const PRESERVE_WHITESPACE_TAGS = ["pre", "textarea", "code"], BLOCK_DEFAULT_STYL
90
92
  ...HTML_HEADER_TAGS,
91
93
  ...HTML_MISC_TAGS
92
94
  };
93
- uniq(
95
+ uniq__default.default(
94
96
  Object.values(ELEMENT_MAP).filter((tag) => "style" in tag).map((tag) => tag.style)
95
97
  );
96
- uniq(
98
+ uniq__default.default(
97
99
  Object.values(HTML_DECORATOR_TAGS)
98
100
  );
99
101
  function blockContentFeatures(blockContentType) {
100
102
  if (!blockContentType)
101
103
  throw new Error("Parameter 'blockContentType' required");
102
104
  const blockType = blockContentType.of.find(findBlockType);
103
- if (!isBlockSchemaType(blockType))
105
+ if (!types.isBlockSchemaType(blockType))
104
106
  throw new Error("'block' type is not defined in this schema (required).");
105
- const ofType = blockType.fields.find(isBlockChildrenObjectField)?.type?.of;
107
+ const ofType = blockType.fields.find(types.isBlockChildrenObjectField)?.type?.of;
106
108
  if (!ofType)
107
109
  throw new Error("No `of` declaration found for blocks `children` field");
108
110
  const spanType = ofType.find(
@@ -113,9 +115,9 @@ function blockContentFeatures(blockContentType) {
113
115
  "No `span` type found in `block` schema type `children` definition"
114
116
  );
115
117
  const inlineObjectTypes = ofType.filter(
116
- (inlineType) => inlineType.name !== "span" && isObjectSchemaType(inlineType)
118
+ (inlineType) => inlineType.name !== "span" && types.isObjectSchemaType(inlineType)
117
119
  ), blockObjectTypes = blockContentType.of.filter(
118
- (memberType) => memberType.name !== blockType.name && isObjectSchemaType(memberType)
120
+ (memberType) => memberType.name !== blockType.name && types.isObjectSchemaType(memberType)
119
121
  );
120
122
  return {
121
123
  styles: resolveEnabledStyles(blockType),
@@ -131,7 +133,7 @@ function blockContentFeatures(blockContentType) {
131
133
  };
132
134
  }
133
135
  function resolveEnabledStyles(blockType) {
134
- const styleField = blockType.fields.find(isBlockStyleObjectField);
136
+ const styleField = blockType.fields.find(types.isBlockStyleObjectField);
135
137
  if (!styleField)
136
138
  throw new Error(
137
139
  "A field with name 'style' is not defined in the block type (required)."
@@ -157,7 +159,7 @@ function resolveEnabledDecorators(spanType) {
157
159
  return spanType.decorators;
158
160
  }
159
161
  function resolveEnabledListItems(blockType) {
160
- const listField = blockType.fields.find(isBlockListObjectField);
162
+ const listField = blockType.fields.find(types.isBlockListObjectField);
161
163
  if (!listField)
162
164
  throw new Error(
163
165
  "A field with name 'list' is not defined in the block type (required)."
@@ -172,7 +174,7 @@ function resolveEnabledListItems(blockType) {
172
174
  function getTitledListValuesFromEnumListOptions(options) {
173
175
  const list = options ? options.list : void 0;
174
176
  return Array.isArray(list) ? list.map(
175
- (item) => isTitledListValue(item) ? item : { title: item, value: item }
177
+ (item) => types.isTitledListValue(item) ? item : { title: item, value: item }
176
178
  ) : [];
177
179
  }
178
180
  const _XPathResult = {
@@ -395,7 +397,7 @@ function flattenNestedBlocks(blocks2) {
395
397
  const flattened = [], traverse = (nodes) => {
396
398
  const toRemove = [];
397
399
  nodes.forEach((node) => {
398
- depth === 0 && flattened.push(node), isPortableTextTextBlock(node) && (depth > 0 && (toRemove.push(node), flattened.push(node)), depth++, traverse(node.children)), node._type === "__block" && (toRemove.push(node), flattened.push(node.block));
400
+ depth === 0 && flattened.push(node), types.isPortableTextTextBlock(node) && (depth > 0 && (toRemove.push(node), flattened.push(node)), depth++, traverse(node.children)), node._type === "__block" && (toRemove.push(node), flattened.push(node.block));
399
401
  }), toRemove.forEach((node) => {
400
402
  nodes.splice(nodes.indexOf(node), 1);
401
403
  }), depth--;
@@ -415,11 +417,11 @@ function isWhiteSpaceChar(text) {
415
417
  }
416
418
  function trimWhitespace(blocks2) {
417
419
  return blocks2.forEach((block) => {
418
- isPortableTextTextBlock(block) && block.children.forEach((child, index) => {
420
+ types.isPortableTextTextBlock(block) && block.children.forEach((child, index) => {
419
421
  if (!isMinimalSpan(child))
420
422
  return;
421
423
  const nextChild = nextSpan(block, index), prevChild = prevSpan(block, index);
422
- index === 0 && (child.text = child.text.replace(/^[^\S\n]+/g, "")), index === block.children.length - 1 && (child.text = child.text.replace(/[^\S\n]+$/g, "")), /\s/.test(child.text.slice(Math.max(0, child.text.length - 1))) && nextChild && isMinimalSpan(nextChild) && /\s/.test(nextChild.text.slice(0, 1)) && (child.text = child.text.replace(/[^\S\n]+$/g, "")), /\s/.test(child.text.slice(0, 1)) && prevChild && isMinimalSpan(prevChild) && /\s/.test(prevChild.text.slice(Math.max(0, prevChild.text.length - 1))) && (child.text = child.text.replace(/^[^\S\n]+/g, "")), child.text || block.children.splice(index, 1), prevChild && isEqual(prevChild.marks, child.marks) && isWhiteSpaceChar(child.text) ? (prevChild.text += " ", block.children.splice(index, 1)) : nextChild && isEqual(nextChild.marks, child.marks) && isWhiteSpaceChar(child.text) && (nextChild.text = ` ${nextChild.text}`, block.children.splice(index, 1));
424
+ index === 0 && (child.text = child.text.replace(/^[^\S\n]+/g, "")), index === block.children.length - 1 && (child.text = child.text.replace(/[^\S\n]+$/g, "")), /\s/.test(child.text.slice(Math.max(0, child.text.length - 1))) && nextChild && isMinimalSpan(nextChild) && /\s/.test(nextChild.text.slice(0, 1)) && (child.text = child.text.replace(/[^\S\n]+$/g, "")), /\s/.test(child.text.slice(0, 1)) && prevChild && isMinimalSpan(prevChild) && /\s/.test(prevChild.text.slice(Math.max(0, prevChild.text.length - 1))) && (child.text = child.text.replace(/^[^\S\n]+/g, "")), child.text || block.children.splice(index, 1), prevChild && isEqual__default.default(prevChild.marks, child.marks) && isWhiteSpaceChar(child.text) ? (prevChild.text += " ", block.children.splice(index, 1)) : nextChild && isEqual__default.default(nextChild.marks, child.marks) && isWhiteSpaceChar(child.text) && (nextChild.text = ` ${nextChild.text}`, block.children.splice(index, 1));
423
425
  });
424
426
  }), blocks2;
425
427
  }
@@ -430,7 +432,7 @@ function ensureRootIsBlocks(blocks2) {
430
432
  if (node._type === "__block")
431
433
  return memo.push(node.block), memo;
432
434
  const lastBlock = memo[memo.length - 1];
433
- if (i > 0 && !isPortableTextTextBlock(original[i - 1]) && isPortableTextTextBlock(lastBlock))
435
+ if (i > 0 && !types.isPortableTextTextBlock(original[i - 1]) && types.isPortableTextTextBlock(lastBlock))
434
436
  return lastBlock.children.push(node), memo;
435
437
  const block = {
436
438
  ...DEFAULT_BLOCK,
@@ -591,7 +593,7 @@ function createGDocsRules(_blockContentType, options) {
591
593
  }
592
594
  function whatwgRNG(length = 16) {
593
595
  const rnds8 = new Uint8Array(length);
594
- return getRandomValues(rnds8), rnds8;
596
+ return getRandomValues__default.default(rnds8), rnds8;
595
597
  }
596
598
  const byteToHex = [];
597
599
  for (let i = 0; i < 256; ++i)
@@ -885,7 +887,7 @@ class HtmlDeserializer {
885
887
  (block) => block._type === "block"
886
888
  ).forEach((block) => {
887
889
  block.markDefs = block.markDefs || [], block.markDefs = block.markDefs.concat(
888
- this._markDefs.filter((def) => flatten(
890
+ this._markDefs.filter((def) => flatten__default.default(
889
891
  block.children.map((child) => child.marks || [])
890
892
  ).includes(def._key))
891
893
  );
@@ -1027,13 +1029,13 @@ function normalizeBlock(node, options = {}) {
1027
1029
  return block.children = block.children.reduce(
1028
1030
  (acc, child) => {
1029
1031
  const previousChild = acc[acc.length - 1];
1030
- return previousChild && isPortableTextSpan(child) && isPortableTextSpan(previousChild) && isEqual(previousChild.marks, child.marks) ? (lastChild && lastChild === child && child.text === "" && block.children.length > 1 || (previousChild.text += child.text), acc) : (acc.push(child), acc);
1032
+ return previousChild && types.isPortableTextSpan(child) && types.isPortableTextSpan(previousChild) && isEqual__default.default(previousChild.marks, child.marks) ? (lastChild && lastChild === child && child.text === "" && block.children.length > 1 || (previousChild.text += child.text), acc) : (acc.push(child), acc);
1031
1033
  },
1032
1034
  []
1033
1035
  ).map((child, index) => {
1034
1036
  if (!child)
1035
1037
  throw new Error("missing child");
1036
- return child._key = `${block._key}${index}`, isPortableTextSpan(child) && (child.marks ? allowedDecorators && (child.marks = child.marks.filter((mark) => {
1038
+ return child._key = `${block._key}${index}`, types.isPortableTextSpan(child) && (child.marks ? allowedDecorators && (child.marks = child.marks.filter((mark) => {
1037
1039
  const isAllowed = allowedDecorators.includes(mark), isUsed = block.markDefs?.some((def) => def._key === mark);
1038
1040
  return isAllowed || isUsed;
1039
1041
  })) : child.marks = [], usedMarkDefs.push(...child.marks)), child;
@@ -1047,10 +1049,8 @@ function htmlToBlocks(html, blockContentType, options = {}) {
1047
1049
  function getBlockContentFeatures(blockContentType) {
1048
1050
  return blockContentFeatures(blockContentType);
1049
1051
  }
1050
- export {
1051
- getBlockContentFeatures,
1052
- htmlToBlocks,
1053
- normalizeBlock,
1054
- randomKey
1055
- };
1056
- //# sourceMappingURL=index.mjs.map
1052
+ exports.getBlockContentFeatures = getBlockContentFeatures;
1053
+ exports.htmlToBlocks = htmlToBlocks;
1054
+ exports.normalizeBlock = normalizeBlock;
1055
+ exports.randomKey = randomKey;
1056
+ //# sourceMappingURL=index.cjs.map