@sanity/block-insert-picker

1.0.121.0.13
dist/index.js
~dist/index.jsModified
+55−57
Index: package/dist/index.js
===================================================================
--- package/dist/index.js
+++ package/dist/index.js
@@ -688,17 +688,16 @@
 	]), hasItems = pickerItems.length > 0, warnedRef = useRef({});
 	useEffect(() => {
 		if (process.env.NODE_ENV === "production") return;
 		let warned = warnedRef.current;
-		if (!itemsContext && !warned.context) {
-			warned.context = !0, console.warn("[@sanity/block-insert-picker] No Portable Text member schema context found and no `arrayTypeName` resolved to an array type — the picker is disabled for this field.");
-			return;
+		if (!itemsContext && !warned.context) warned.context = !0, console.warn("[@sanity/block-insert-picker] No Portable Text member schema context found and no `arrayTypeName` resolved to an array type — the picker is disabled for this field.");
+		else {
+			if (itemsContext && items && !warned.unknown) {
+				let unknown = unknownMetadataTypes(itemsContext, items);
+				unknown.length > 0 && (warned.unknown = !0, console.warn(`[@sanity/block-insert-picker] \`items\` metadata references types that are not members of this array: ${unknown.join(", ")}`));
+			}
+			itemsContext && !hasItems && !warned.empty && (warned.empty = !0, console.warn("[@sanity/block-insert-picker] No insertable items: the array has no non-text-block members (and `resolveItems` added none) — the picker is disabled for this field."));
 		}
-		if (itemsContext && items && !warned.unknown) {
-			let unknown = unknownMetadataTypes(itemsContext, items);
-			unknown.length > 0 && (warned.unknown = !0, console.warn(`[@sanity/block-insert-picker] \`items\` metadata references types that are not members of this array: ${unknown.join(", ")}`));
-		}
-		itemsContext && !hasItems && !warned.empty && (warned.empty = !0, console.warn("[@sanity/block-insert-picker] No insertable items: the array has no non-text-block members (and `resolveItems` added none) — the picker is disabled for this field."));
 	}, [
 		hasItems,
 		items,
 		itemsContext
@@ -719,66 +718,65 @@
 		let offsets = [], total = 0;
 		for (let section of sections) offsets.push(total), total += section.items.length;
 		return offsets;
 	}, [sections]), showGroupHeaders = sections.length > 1, insertInFlightRef = useRef(!1), insertItemAtIndex = async (current, idx) => {
-		if (insertInFlightRef.current) {
-			dispatch({ type: "close" });
-			return;
-		}
-		insertInFlightRef.current = !0;
-		try {
-			let visible = flattenSections(groupPickerItems(filterItems(pickerItems, current.mode === "slash" ? bareQuery(current.query) : current.query))), chosen = visible[Math.max(0, Math.min(visible.length - 1, idx))];
-			if (dispatch({ type: "close" }), !chosen) return;
-			if (chosen.action.type === "custom") {
-				sendCleanupQuery(editor, {
+		if (insertInFlightRef.current) dispatch({ type: "close" });
+		else {
+			insertInFlightRef.current = !0;
+			try {
+				let visible = flattenSections(groupPickerItems(filterItems(pickerItems, current.mode === "slash" ? bareQuery(current.query) : current.query))), chosen = visible[Math.max(0, Math.min(visible.length - 1, idx))];
+				if (dispatch({ type: "close" }), !chosen) return;
+				if (chosen.action.type === "custom") {
+					sendCleanupQuery(editor, {
+						anchorBlockKey: current.anchorBlockKey,
+						mode: current.mode,
+						query: current.query
+					}), chosen.action.onSelect({
+						closePicker: () => dispatch({ type: "close" }),
+						editor
+					});
+					return;
+				}
+				let blockType = chosen.action.blockType, initialValue, schemaType = memberTypeFor(itemsContext, schema, blockType);
+				schemaType && (initialValue = await resolveInitialValueForType(schemaType, {}) ?? void 0);
+				let snapshot = editor.getSnapshot();
+				if (!snapshot.context.value.some((b) => b._key === current.anchorBlockKey)) {
+					toast.push({
+						closable: !0,
+						description: labels.insertAnchorRemoved,
+						status: "warning",
+						title: labels.insertError
+					});
+					return;
+				}
+				insertPickerItem({
 					anchorBlockKey: current.anchorBlockKey,
+					blockType,
+					editor,
+					initialValue,
+					keyGenerator: () => snapshot.context.keyGenerator(),
 					mode: current.mode,
+					onInsertedKey: (key) => {
+						(chosen.openOnInsert ?? openOnInsert) && openBlockOnInsert(key), onInsert?.({
+							blockKey: key,
+							blockType,
+							mode: current.mode,
+							query: current.query,
+							via: "picker"
+						});
+					},
 					query: current.query
-				}), chosen.action.onSelect({
-					closePicker: () => dispatch({ type: "close" }),
-					editor
 				});
-				return;
-			}
-			let blockType = chosen.action.blockType, initialValue, schemaType = memberTypeFor(itemsContext, schema, blockType);
-			schemaType && (initialValue = await resolveInitialValueForType(schemaType, {}) ?? void 0);
-			let snapshot = editor.getSnapshot();
-			if (!snapshot.context.value.some((b) => b._key === current.anchorBlockKey)) {
+			} catch (error) {
 				toast.push({
 					closable: !0,
-					description: labels.insertAnchorRemoved,
-					status: "warning",
+					description: error instanceof Error ? error.message : String(error),
+					status: "error",
 					title: labels.insertError
 				});
-				return;
+			} finally {
+				insertInFlightRef.current = !1;
 			}
-			insertPickerItem({
-				anchorBlockKey: current.anchorBlockKey,
-				blockType,
-				editor,
-				initialValue,
-				keyGenerator: () => snapshot.context.keyGenerator(),
-				mode: current.mode,
-				onInsertedKey: (key) => {
-					(chosen.openOnInsert ?? openOnInsert) && openBlockOnInsert(key), onInsert?.({
-						blockKey: key,
-						blockType,
-						mode: current.mode,
-						query: current.query,
-						via: "picker"
-					});
-				},
-				query: current.query
-			});
-		} catch (error) {
-			toast.push({
-				closable: !0,
-				description: error instanceof Error ? error.message : String(error),
-				status: "error",
-				title: labels.insertError
-			});
-		} finally {
-			insertInFlightRef.current = !1;
 		}
 	}, selectItemAtIndex = (idx) => {
 		let current = stateRef.current;
 		current.mode !== "closed" && insertItemAtIndex(current, idx);