@portabletext/editor 1.4.1 → 1.5.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.
package/lib/index.js CHANGED
@@ -1,10 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: !0 });
3
- var types = require("@sanity/types"), jsxRuntime = require("react/jsx-runtime"), isEqual = require("lodash/isEqual.js"), noop = require("lodash/noop.js"), react = require("react"), slate = require("slate"), slateReact = require("slate-react"), debug$m = require("debug"), reactCompilerRuntime = require("react-compiler-runtime"), styledComponents = require("styled-components"), uniq = require("lodash/uniq.js"), rxjs = require("rxjs"), xstate = require("xstate"), schema = require("@sanity/schema"), patches = require("@portabletext/patches"), get = require("lodash/get.js"), isUndefined = require("lodash/isUndefined.js"), omitBy = require("lodash/omitBy.js"), flatten = require("lodash/flatten.js"), isHotkeyEsm = require("is-hotkey-esm"), blockTools = require("@sanity/block-tools"), isPlainObject = require("lodash/isPlainObject.js"), throttle = require("lodash/throttle.js"), useEffectEvent = require("use-effect-event"), debounce = require("lodash/debounce.js"), content = require("@sanity/util/content"), react$1 = require("@xstate/react");
3
+ var types = require("@sanity/types"), schema = require("@sanity/schema"), startCase = require("lodash.startcase"), jsxRuntime = require("react/jsx-runtime"), isEqual = require("lodash/isEqual.js"), noop = require("lodash/noop.js"), react = require("react"), slate = require("slate"), slateReact = require("slate-react"), debug$m = require("debug"), reactCompilerRuntime = require("react-compiler-runtime"), styledComponents = require("styled-components"), uniq = require("lodash/uniq.js"), rxjs = require("rxjs"), xstate = require("xstate"), patches = require("@portabletext/patches"), get = require("lodash/get.js"), isUndefined = require("lodash/isUndefined.js"), omitBy = require("lodash/omitBy.js"), flatten = require("lodash/flatten.js"), isHotkeyEsm = require("is-hotkey-esm"), blockTools = require("@sanity/block-tools"), isPlainObject = require("lodash/isPlainObject.js"), throttle = require("lodash/throttle.js"), useEffectEvent = require("use-effect-event"), debounce = require("lodash/debounce.js"), content = require("@sanity/util/content"), react$1 = require("@xstate/react");
4
4
  function _interopDefaultCompat(e) {
5
5
  return e && typeof e == "object" && "default" in e ? e : { default: e };
6
6
  }
7
- var isEqual__default = /* @__PURE__ */ _interopDefaultCompat(isEqual), noop__default = /* @__PURE__ */ _interopDefaultCompat(noop), debug__default = /* @__PURE__ */ _interopDefaultCompat(debug$m), uniq__default = /* @__PURE__ */ _interopDefaultCompat(uniq), get__default = /* @__PURE__ */ _interopDefaultCompat(get), isUndefined__default = /* @__PURE__ */ _interopDefaultCompat(isUndefined), omitBy__default = /* @__PURE__ */ _interopDefaultCompat(omitBy), flatten__default = /* @__PURE__ */ _interopDefaultCompat(flatten), isPlainObject__default = /* @__PURE__ */ _interopDefaultCompat(isPlainObject), throttle__default = /* @__PURE__ */ _interopDefaultCompat(throttle), debounce__default = /* @__PURE__ */ _interopDefaultCompat(debounce);
7
+ var startCase__default = /* @__PURE__ */ _interopDefaultCompat(startCase), isEqual__default = /* @__PURE__ */ _interopDefaultCompat(isEqual), noop__default = /* @__PURE__ */ _interopDefaultCompat(noop), debug__default = /* @__PURE__ */ _interopDefaultCompat(debug$m), uniq__default = /* @__PURE__ */ _interopDefaultCompat(uniq), get__default = /* @__PURE__ */ _interopDefaultCompat(get), isUndefined__default = /* @__PURE__ */ _interopDefaultCompat(isUndefined), omitBy__default = /* @__PURE__ */ _interopDefaultCompat(omitBy), flatten__default = /* @__PURE__ */ _interopDefaultCompat(flatten), isPlainObject__default = /* @__PURE__ */ _interopDefaultCompat(isPlainObject), throttle__default = /* @__PURE__ */ _interopDefaultCompat(throttle), debounce__default = /* @__PURE__ */ _interopDefaultCompat(debounce);
8
8
  function defineBehavior(behavior) {
9
9
  return behavior;
10
10
  }
@@ -245,6 +245,114 @@ function createMarkdownBehaviors(config) {
245
245
  };
246
246
  return [automaticStyleOnSpace, clearStyleOnBackspace, automaticListOnSpace];
247
247
  }
248
+ function getPortableTextMemberSchemaTypes(portableTextType) {
249
+ if (!portableTextType)
250
+ throw new Error("Parameter 'portabletextType' missing (required)");
251
+ const blockType = portableTextType.of?.find(findBlockType);
252
+ if (!blockType)
253
+ throw new Error("Block type is not defined in this schema (required)");
254
+ const childrenField = blockType.fields?.find((field) => field.name === "children");
255
+ if (!childrenField)
256
+ throw new Error("Children field for block type found in schema (required)");
257
+ const ofType = childrenField.type.of;
258
+ if (!ofType)
259
+ throw new Error("Valid types for block children not found in schema (required)");
260
+ const spanType = ofType.find((memberType) => memberType.name === "span");
261
+ if (!spanType)
262
+ throw new Error("Span type not found in schema (required)");
263
+ const inlineObjectTypes = ofType.filter((memberType) => memberType.name !== "span") || [], blockObjectTypes = portableTextType.of?.filter((field) => field.name !== blockType.name) || [];
264
+ return {
265
+ styles: resolveEnabledStyles(blockType),
266
+ decorators: resolveEnabledDecorators(spanType),
267
+ lists: resolveEnabledListItems(blockType),
268
+ block: blockType,
269
+ span: spanType,
270
+ portableText: portableTextType,
271
+ inlineObjects: inlineObjectTypes,
272
+ blockObjects: blockObjectTypes,
273
+ annotations: spanType.annotations
274
+ };
275
+ }
276
+ function resolveEnabledStyles(blockType) {
277
+ const styleField = blockType.fields?.find((btField) => btField.name === "style");
278
+ if (!styleField)
279
+ throw new Error("A field with name 'style' is not defined in the block type (required).");
280
+ const textStyles = styleField.type.options?.list && styleField.type.options.list?.filter((style) => style.value);
281
+ if (!textStyles || textStyles.length === 0)
282
+ throw new Error("The style fields need at least one style defined. I.e: {title: 'Normal', value: 'normal'}.");
283
+ return textStyles;
284
+ }
285
+ function resolveEnabledDecorators(spanType) {
286
+ return spanType.decorators;
287
+ }
288
+ function resolveEnabledListItems(blockType) {
289
+ const listField = blockType.fields?.find((btField) => btField.name === "listItem");
290
+ if (!listField)
291
+ throw new Error("A field with name 'listItem' is not defined in the block type (required).");
292
+ const listItems = listField.type.options?.list && listField.type.options.list.filter((list) => list.value);
293
+ if (!listItems)
294
+ throw new Error("The list field need at least to be an empty array");
295
+ return listItems;
296
+ }
297
+ function findBlockType(type) {
298
+ return type.type ? findBlockType(type.type) : type.name === "block" ? type : null;
299
+ }
300
+ function defineSchema(definition) {
301
+ return definition;
302
+ }
303
+ function compileSchemaDefinition(definition) {
304
+ const blockObjects = definition?.blockObjects?.map((blockObject) => types.defineType({
305
+ type: "object",
306
+ name: blockObject.name,
307
+ title: blockObject.title,
308
+ icon: blockObject.icon,
309
+ fields: []
310
+ })) ?? [], inlineObjects = definition?.inlineObjects?.map((inlineObject) => types.defineType({
311
+ type: "object",
312
+ name: inlineObject.name,
313
+ title: inlineObject.title,
314
+ icon: inlineObject.icon,
315
+ fields: []
316
+ })) ?? [], portableTextSchema = types.defineField({
317
+ type: "array",
318
+ name: "portable-text",
319
+ of: [...blockObjects.map((blockObject) => ({
320
+ type: blockObject.name
321
+ })), {
322
+ type: "block",
323
+ name: "block",
324
+ of: inlineObjects.map((inlineObject) => ({
325
+ type: inlineObject.name
326
+ })),
327
+ marks: {
328
+ decorators: definition?.decorators?.map((decorator) => ({
329
+ title: decorator.title ?? startCase__default.default(decorator.name),
330
+ value: decorator.name,
331
+ icon: decorator.icon
332
+ })) ?? [],
333
+ annotations: definition?.annotations?.map((annotation) => ({
334
+ name: annotation.name,
335
+ type: "object",
336
+ title: annotation.title,
337
+ icon: annotation.icon
338
+ })) ?? []
339
+ },
340
+ lists: definition?.lists?.map((list) => ({
341
+ value: list.name,
342
+ title: list.title ?? startCase__default.default(list.name),
343
+ icon: list.icon
344
+ })) ?? [],
345
+ styles: definition?.styles?.map((style) => ({
346
+ value: style.name,
347
+ title: style.title ?? startCase__default.default(style.name),
348
+ icon: style.icon
349
+ })) ?? []
350
+ }]
351
+ }), schema$1 = schema.Schema.compile({
352
+ types: [portableTextSchema, ...blockObjects, ...inlineObjects]
353
+ }).get("portable-text");
354
+ return getPortableTextMemberSchemaTypes(schema$1);
355
+ }
248
356
  const rootName = "sanity-pte:";
249
357
  debug__default.default(rootName);
250
358
  function debugWithName(name) {
@@ -935,58 +1043,6 @@ function DefaultAnnotation(props) {
935
1043
  return $[3] !== handleClick || $[4] !== props.children ? (t2 = /* @__PURE__ */ jsxRuntime.jsx("span", { style: t1, onClick: handleClick, children: props.children }), $[3] = handleClick, $[4] = props.children, $[5] = t2) : t2 = $[5], t2;
936
1044
  }
937
1045
  DefaultAnnotation.displayName = "DefaultAnnotation";
938
- function getPortableTextMemberSchemaTypes(portableTextType) {
939
- if (!portableTextType)
940
- throw new Error("Parameter 'portabletextType' missing (required)");
941
- const blockType = portableTextType.of?.find(findBlockType);
942
- if (!blockType)
943
- throw new Error("Block type is not defined in this schema (required)");
944
- const childrenField = blockType.fields?.find((field) => field.name === "children");
945
- if (!childrenField)
946
- throw new Error("Children field for block type found in schema (required)");
947
- const ofType = childrenField.type.of;
948
- if (!ofType)
949
- throw new Error("Valid types for block children not found in schema (required)");
950
- const spanType = ofType.find((memberType) => memberType.name === "span");
951
- if (!spanType)
952
- throw new Error("Span type not found in schema (required)");
953
- const inlineObjectTypes = ofType.filter((memberType) => memberType.name !== "span") || [], blockObjectTypes = portableTextType.of?.filter((field) => field.name !== blockType.name) || [];
954
- return {
955
- styles: resolveEnabledStyles(blockType),
956
- decorators: resolveEnabledDecorators(spanType),
957
- lists: resolveEnabledListItems(blockType),
958
- block: blockType,
959
- span: spanType,
960
- portableText: portableTextType,
961
- inlineObjects: inlineObjectTypes,
962
- blockObjects: blockObjectTypes,
963
- annotations: spanType.annotations
964
- };
965
- }
966
- function resolveEnabledStyles(blockType) {
967
- const styleField = blockType.fields?.find((btField) => btField.name === "style");
968
- if (!styleField)
969
- throw new Error("A field with name 'style' is not defined in the block type (required).");
970
- const textStyles = styleField.type.options?.list && styleField.type.options.list?.filter((style) => style.value);
971
- if (!textStyles || textStyles.length === 0)
972
- throw new Error("The style fields need at least one style defined. I.e: {title: 'Normal', value: 'normal'}.");
973
- return textStyles;
974
- }
975
- function resolveEnabledDecorators(spanType) {
976
- return spanType.decorators;
977
- }
978
- function resolveEnabledListItems(blockType) {
979
- const listField = blockType.fields?.find((btField) => btField.name === "listItem");
980
- if (!listField)
981
- throw new Error("A field with name 'listItem' is not defined in the block type (required).");
982
- const listItems = listField.type.options?.list && listField.type.options.list.filter((list) => list.value);
983
- if (!listItems)
984
- throw new Error("The list field need at least to be an empty array");
985
- return listItems;
986
- }
987
- function findBlockType(type) {
988
- return type.type ? findBlockType(type.type) : type.name === "block" ? type : null;
989
- }
990
1046
  function compileType(rawType) {
991
1047
  return schema.Schema.compile({
992
1048
  name: "blockTypeSchema",
@@ -6196,25 +6252,24 @@ const debug = debugWithName("component:Editable"), PLACEHOLDER_STYLE = {
6196
6252
  });
6197
6253
  PortableTextEditable.displayName = "ForwardRef(PortableTextEditable)";
6198
6254
  function useEditor(config) {
6199
- const $ = reactCompilerRuntime.c(8);
6255
+ const $ = reactCompilerRuntime.c(7);
6200
6256
  let t0;
6201
- $[0] !== config.schema ? (t0 = config.schema.hasOwnProperty("jsonType") ? config.schema : compileType(config.schema), $[0] = config.schema, $[1] = t0) : t0 = $[1];
6202
- let t1;
6203
- $[2] !== t0 ? (t1 = getPortableTextMemberSchemaTypes(t0), $[2] = t0, $[3] = t1) : t1 = $[3];
6204
- const schema2 = t1, t2 = config.keyGenerator ?? defaultKeyGenerator;
6205
- let t3;
6206
- return $[4] !== config.behaviors || $[5] !== t2 || $[6] !== schema2 ? (t3 = {
6257
+ $[0] !== config.schemaDefinition || $[1] !== config.schema ? (t0 = config.schemaDefinition ? compileSchemaDefinition(config.schemaDefinition) : getPortableTextMemberSchemaTypes(config.schema.hasOwnProperty("jsonType") ? config.schema : compileType(config.schema)), $[0] = config.schemaDefinition, $[1] = config.schema, $[2] = t0) : t0 = $[2];
6258
+ const schema2 = t0, t1 = config.keyGenerator ?? defaultKeyGenerator;
6259
+ let t2;
6260
+ return $[3] !== config.behaviors || $[4] !== t1 || $[5] !== schema2 ? (t2 = {
6207
6261
  input: {
6208
6262
  behaviors: config.behaviors,
6209
- keyGenerator: t2,
6263
+ keyGenerator: t1,
6210
6264
  schema: schema2
6211
6265
  }
6212
- }, $[4] = config.behaviors, $[5] = t2, $[6] = schema2, $[7] = t3) : t3 = $[7], react$1.useActorRef(editorMachine, t3);
6266
+ }, $[3] = config.behaviors, $[4] = t1, $[5] = schema2, $[6] = t2) : t2 = $[6], react$1.useActorRef(editorMachine, t2);
6213
6267
  }
6214
6268
  exports.PortableTextEditable = PortableTextEditable;
6215
6269
  exports.PortableTextEditor = PortableTextEditor;
6216
6270
  exports.createMarkdownBehaviors = createMarkdownBehaviors;
6217
6271
  exports.defineBehavior = defineBehavior;
6272
+ exports.defineSchema = defineSchema;
6218
6273
  exports.editorMachine = editorMachine;
6219
6274
  exports.keyGenerator = defaultKeyGenerator;
6220
6275
  exports.useEditor = useEditor;