Shared types, utilities, and action system for Story Adventure.
This package contains the core logic and type definitions used by both the Story Adventure editor and viewer. It includes:
types.ts) - TypeScript interfaces for Story, Section, Choice, Action, etc.actions.ts) - Execution engine for story actions (SET, IF_SET_DO, ADD_CHOICE, etc.)utils.ts) - Helper functions for text extraction and file namingvariables.ts) - Variable replacement in text (${variable} syntax)storage.ts) - IndexedDB utilities for saving/loading storiespnpm add @story-adventure/shared
import type { Story, Section, Action } from '@story-adventure/shared';
const story: Story = {
sections: {
'1': {
id: '1',
text_lines: ['Hello ${name}!'],
next: [{ text: 'Continue', next: '2' }],
},
},
state: {
variables: { name: 'Alice' },
},
};
import { execute_actions, supported_actions } from '@story-adventure/shared';
// Execute actions from a section's script
execute_actions(story, [
{ action: 'SET', parameters: ['score', '100'] },
{ action: 'ADD_CHOICE', parameters: ['2', 'Go to section 2'] },
]);
import { replace_variables } from '@story-adventure/shared';
const text = replace_variables('Hello ${name}', { name: 'Alice' });
// Returns: "Hello Alice"
import { get_text_from_section, get_file_safe_title } from '@story-adventure/shared';
const text = get_text_from_section(section, story.state?.variables);
const filename = get_file_safe_title(story);
The action system supports the following action types:
See the Story Format documentation for detailed action specifications.
Run tests with:
pnpm test
See root LICENSE file.