histoire
Version:
A fast story book powered by Vite
25 lines (24 loc) • 1.86 kB
JavaScript
import { HistoireSdkError, isHistoireProjectFileOrigin, validateHistoireTarget, validateWireValue } from '@histoire/protocol';
import { lookupStory, lookupTarget } from '../../runtime/catalog/lookup.js';
/** Resolve registered private physical source; no arbitrary file/path/URL arguments. */
export function resolveEmbedEditorTarget(snapshot, descriptor, value) {
if (descriptor.mode !== 'dev' || !descriptor.capabilities.openInEditor.available)
throw new HistoireSdkError('CAPABILITY_UNAVAILABLE', 'Editor is available only in dev');
validateWireValue(value, { kind: 'request', name: 'openInEditor' });
const input = value;
if (!input || Object.keys(input).some(key => !['target', 'epoch', 'revision'].includes(key)))
throw new HistoireSdkError('INVALID_ARGUMENT', 'Invalid editor request fields');
validateHistoireTarget(input.target);
if (input.epoch !== descriptor.epoch || input.revision !== descriptor.revision || snapshot.epoch !== input.epoch || snapshot.revision !== input.revision)
throw new HistoireSdkError('STALE_REVISION', 'Editor source publication changed');
const { story } = input.target.variantId === null ? lookupStory(snapshot, input.target.storyId) : lookupTarget(snapshot, input.target.storyId, input.target.variantId);
if (!isHistoireProjectFileOrigin(story.fileOrigin))
throw new HistoireSdkError('CAPABILITY_UNAVAILABLE', 'Editor is unavailable for non-project stories');
const content = snapshot.contents.get(story.filePath);
if (story.docsOnly && content?.docs?.absolutePath)
return content.docs.absolutePath;
const source = content?.source;
if (source?.kind !== 'file' || !source.absolutePath)
throw new HistoireSdkError('CAPABILITY_UNAVAILABLE', 'Collected target has no physical editor source');
return source.absolutePath;
}