@portabletext/editor 1.44.12 → 1.44.13

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.
@@ -82,21 +82,58 @@ function resolveEnabledListItems(blockType) {
82
82
  function findBlockType(type) {
83
83
  return type.type ? findBlockType(type.type) : type.name === "block" ? type : null;
84
84
  }
85
+ const defaultKeyGenerator = () => randomKey(12), getByteHexTable = /* @__PURE__ */ (() => {
86
+ let table;
87
+ return () => {
88
+ if (table)
89
+ return table;
90
+ table = [];
91
+ for (let i = 0; i < 256; ++i)
92
+ table[i] = (i + 256).toString(16).slice(1);
93
+ return table;
94
+ };
95
+ })();
96
+ function whatwgRNG(length = 16) {
97
+ const rnds8 = new Uint8Array(length);
98
+ return getRandomValues(rnds8), rnds8;
99
+ }
100
+ function randomKey(length) {
101
+ const table = getByteHexTable();
102
+ return whatwgRNG(length).reduce((str, n) => str + table[n], "").slice(0, length);
103
+ }
85
104
  function defineSchema(definition) {
86
105
  return definition;
87
106
  }
107
+ const temporaryImageName = `tmp-${defaultKeyGenerator()}-image`, temporaryUrlName = `tmp-${defaultKeyGenerator()}-url`, temporaryObjectNames = {
108
+ image: temporaryImageName,
109
+ url: temporaryUrlName
110
+ }, objectNames = {
111
+ [temporaryImageName]: "image",
112
+ [temporaryUrlName]: "url"
113
+ }, defaultObjectTitles = {
114
+ image: "Image",
115
+ url: "URL"
116
+ };
88
117
  function compileSchemaDefinition(definition) {
89
118
  const blockObjects = definition?.blockObjects?.map((blockObject) => defineType({
90
119
  type: "object",
91
120
  // Very naive way to work around `SanitySchema.compile` adding default
92
- // fields to objects with the name `image`
93
- name: blockObject.name === "image" ? "tmp-image" : blockObject.name,
94
- title: blockObject.name === "image" && blockObject.title === void 0 ? "Image" : blockObject.title,
121
+ // fields to objects with certain names.
122
+ name: temporaryObjectNames[blockObject.name] ?? blockObject.name,
123
+ title: blockObject.title === void 0 ? (
124
+ // This avoids the default title which is a title case of the object name
125
+ defaultObjectTitles[blockObject.name]
126
+ ) : blockObject.title,
95
127
  fields: []
96
128
  })) ?? [], inlineObjects = definition?.inlineObjects?.map((inlineObject) => defineType({
97
129
  type: "object",
98
- name: inlineObject.name,
99
- title: inlineObject.title,
130
+ // Very naive way to work around `SanitySchema.compile` adding default
131
+ // fields to objects with certain names.
132
+ name: temporaryObjectNames[inlineObject.name] ?? inlineObject.name,
133
+ title: inlineObject.title === void 0 ? (
134
+ // This avoids the default title which is a title case of the object name
135
+ defaultObjectTitles[inlineObject.name]
136
+ ) : inlineObject.title,
100
137
  fields: []
101
138
  })) ?? [], portableTextSchema = defineField({
102
139
  type: "array",
@@ -134,14 +171,18 @@ function compileSchemaDefinition(definition) {
134
171
  }).get("portable-text"), pteSchema = createEditorSchema(schema);
135
172
  return {
136
173
  ...pteSchema,
137
- blockObjects: pteSchema.blockObjects.map((blockObject) => blockObject.name === "tmp-image" ? {
174
+ blockObjects: pteSchema.blockObjects.map((blockObject) => objectNames[blockObject.name] !== void 0 ? {
138
175
  ...blockObject,
139
- name: "image",
176
+ name: objectNames[blockObject.name],
140
177
  type: {
141
178
  ...blockObject.type,
142
- name: "image"
179
+ name: objectNames[blockObject.name]
143
180
  }
144
- } : blockObject)
181
+ } : blockObject),
182
+ inlineObjects: pteSchema.inlineObjects.map((inlineObject) => objectNames[inlineObject.name] !== void 0 ? {
183
+ ...inlineObject,
184
+ name: objectNames[inlineObject.name]
185
+ } : inlineObject)
145
186
  };
146
187
  }
147
188
  const rootName = "sanity-pte:";
@@ -1653,25 +1694,6 @@ function isRedoing(editor) {
1653
1694
  function setIsRedoing(editor, isRedoing2) {
1654
1695
  IS_REDOING.set(editor, isRedoing2);
1655
1696
  }
1656
- const defaultKeyGenerator = () => randomKey(12), getByteHexTable = /* @__PURE__ */ (() => {
1657
- let table;
1658
- return () => {
1659
- if (table)
1660
- return table;
1661
- table = [];
1662
- for (let i = 0; i < 256; ++i)
1663
- table[i] = (i + 256).toString(16).slice(1);
1664
- return table;
1665
- };
1666
- })();
1667
- function whatwgRNG(length = 16) {
1668
- const rnds8 = new Uint8Array(length);
1669
- return getRandomValues(rnds8), rnds8;
1670
- }
1671
- function randomKey(length) {
1672
- const table = getByteHexTable();
1673
- return whatwgRNG(length).reduce((str, n) => str + table[n], "").slice(0, length);
1674
- }
1675
1697
  const CURRENT_ACTION_ID = /* @__PURE__ */ new WeakMap();
1676
1698
  function withApplyingBehaviorActions(editor, fn) {
1677
1699
  CURRENT_ACTION_ID.set(editor, defaultKeyGenerator()), Editor.withoutNormalizing(editor, fn), CURRENT_ACTION_ID.set(editor, void 0);