@remotion/studio
Version:
APIs for interacting with the Remotion Studio
92 lines (91 loc) • 4.94 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.ZodMatrixEditor = void 0;
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = __importStar(require("react"));
const react_2 = require("react");
const get_zod_if_possible_1 = require("../../get-zod-if-possible");
const Fieldset_1 = require("./Fieldset");
const SchemaLabel_1 = require("./SchemaLabel");
const SchemaSeparationLine_1 = require("./SchemaSeparationLine");
const SchemaVerticalGuide_1 = require("./SchemaVerticalGuide");
const zod_schema_type_1 = require("./zod-schema-type");
const zod_schema_type_2 = require("./zod-schema-type");
const ZodArrayItemEditor_1 = require("./ZodArrayItemEditor");
const ZodFieldValidation_1 = require("./ZodFieldValidation");
const rowStyle = {
display: 'flex',
flexDirection: 'row',
width: '100%',
};
const ZodMatrixEditor = ({ schema, jsonPath, setValue, value, onRemove, mayPad }) => {
const onChange = (0, react_2.useCallback)((updater, { shouldSave }) => {
setValue(updater, { shouldSave });
}, [setValue]);
const zodValidation = (0, react_1.useMemo)(() => (0, zod_schema_type_1.zodSafeParse)(schema, value), [schema, value]);
const [expanded, setExpanded] = (0, react_1.useState)(true);
const arrayElement = (0, zod_schema_type_2.getArrayElement)(schema);
const suffix = (0, react_1.useMemo)(() => {
return expanded ? ' [' : ' [...] ';
}, [expanded]);
const z = (0, get_zod_if_possible_1.useZodIfPossible)();
if (!z) {
throw new Error('expected zod');
}
const dimensions = Math.sqrt(value.length);
if (!Number.isInteger(dimensions)) {
throw new Error('Invalid matrix');
}
const chunkedItems = (0, react_1.useMemo)(() => {
return value.reduce((acc, item, index) => {
const chunkIndex = Math.floor(index / dimensions);
acc[chunkIndex] = acc[chunkIndex] || [];
acc[chunkIndex].push(item);
return acc;
}, []);
}, [value, dimensions]);
return (jsx_runtime_1.jsxs(Fieldset_1.Fieldset, { shouldPad: mayPad, children: [
jsx_runtime_1.jsx(SchemaLabel_1.SchemaLabel, { jsonPath: jsonPath, onRemove: onRemove, suffix: suffix, valid: zodValidation.success, handleClick: () => setExpanded(!expanded) }), expanded ? (jsx_runtime_1.jsxs(SchemaVerticalGuide_1.SchemaVerticalGuide, { isRoot: false, children: [chunkedItems.map((row, rowIndex) => {
return (jsx_runtime_1.jsx(react_1.default.Fragment
// eslint-disable-next-line react/no-array-index-key
, { children: jsx_runtime_1.jsx("div", { style: rowStyle, children: row.map((item, _index) => {
const actualIndex = rowIndex * dimensions + _index;
return (jsx_runtime_1.jsx("div", { style: { flex: 1 }, children: jsx_runtime_1.jsx(ZodArrayItemEditor_1.ZodArrayItemEditor, { onChange: onChange, value: item, elementSchema: arrayElement, index: actualIndex, jsonPath: jsonPath, mayPad: mayPad, mayRemove: false }) }, _index));
}) }) }, rowIndex));
}), value.length === 0 ? (jsx_runtime_1.jsx(SchemaSeparationLine_1.SchemaArrayItemSeparationLine, { schema: schema, index: 0, onChange: onChange, isLast: true, showAddButton: true })) : null] })) : null, jsx_runtime_1.jsx(ZodFieldValidation_1.ZodFieldValidation, { path: jsonPath, zodValidation: zodValidation })
] }));
};
exports.ZodMatrixEditor = ZodMatrixEditor;