Skip to content

Commit

Permalink
add ink.getVariables method
Browse files Browse the repository at this point in the history
  • Loading branch information
technix committed Aug 25, 2024
1 parent a61129e commit cd8bac5
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,12 @@ Returns value of specified Ink variable.

Event: `'ink/getVariable', { name: variableName }`

#### atrament.ink.getVariables()

Returns all variables and their values as a key-value object.

Event: `'ink/getVariables', inkVariablesObject`

#### atrament.ink.setVariable(variableName, value)

Sets value of specified Ink variable.
Expand Down
18 changes: 17 additions & 1 deletion __tests__/components/ink.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ const mockInkStoryInstance = {
return `${fn} result: ${args[0] + args[1]}`;
},
variablesState: {
var1: 'var1-value'
var1: 'var1-value',
var2: 'var2-value'
},
ObserveVariable() {
return true;
Expand All @@ -61,6 +62,11 @@ const mockInkStoryInstance = {
}
};

const inkVars = Object.keys(mockInkStoryInstance.variablesState);
const globalVars = new Map();
inkVars.forEach((k) => globalVars.set(k, true));
mockInkStoryInstance.variablesState._globalVariables = globalVars; /* eslint-disable-line no-underscore-dangle */

const MockInkStory = jest.fn((content) => {
mockInkStoryInstance.content = content;
return mockInkStoryInstance;
Expand Down Expand Up @@ -160,6 +166,16 @@ describe('components/ink', () => {
expect(emit).toHaveBeenCalledWith('ink/getVariable', { name: 'var1', value: expectedValue });
});

test('getVariables', () => {
const expectedValue = {
var1: 'var1-value',
var2: 'var2-value'
};
const result = ink.getVariables();
expect(result).toEqual(expectedValue);
expect(emit).toHaveBeenCalledWith('ink/getVariables', result);
});

test('setVariable', () => {
ink.setVariable('varTest', 10);
const expectedValue = mockInkStoryInstance.variablesState.varTest;
Expand Down
9 changes: 9 additions & 0 deletions src/components/ink.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,15 @@ export default {
emit('ink/getVariable', { name, value });
return value;
},
getVariables: () => {
const varState = inkStory.variablesState;
const inkVars = {};
varState._globalVariables.forEach(/* eslint-disable-line no-underscore-dangle */
(value, key) => { inkVars[key] = varState[key]; }
);
emit('ink/getVariables', inkVars);
return inkVars;
},
setVariable: (name, value) => {
inkStory.variablesState[name] = value;
emit('ink/setVariable', { name, value });
Expand Down

0 comments on commit cd8bac5

Please sign in to comment.