diff --git a/packages/core/src/part.mjs b/packages/core/src/part.mjs index 98db5271154..212235ffaba 100644 --- a/packages/core/src/part.mjs +++ b/packages/core/src/part.mjs @@ -197,6 +197,13 @@ Part.prototype.shorthand = function () { addCut: this.addCut, removeCut: this.removeCut, } + // Add top-level store methods and add a part name parameter + const partName = this.name + for (const [key, method] of Object.entries(this.context.store)) { + if (typeof method === 'function') shorthand[key] = function(...args) { + return method(partName, ...args) + } + } // We'll need this let self = this diff --git a/packages/core/tests/store.test.mjs b/packages/core/tests/store.test.mjs index b15a2bd7b4d..ab013d2dda3 100644 --- a/packages/core/tests/store.test.mjs +++ b/packages/core/tests/store.test.mjs @@ -65,5 +65,33 @@ describe('Store', () => { expect(pattern.store.get("test.message.warning")).to.equal("hello warning") expect(pattern.store.get("test.message.info")).to.equal("hello info") }); + + it("Should make top-level plugin methods available via shorthand", () => { + const plugin = { + name: 'test', + version: 1, + store: [ + ['methodA', function(store, name, msg) { + store.set(['test', name, 'a'], msg) + }], + ['methodB', function(store, name, msg) { + store.set(['test', name, 'b'], msg) + }], + ] + } + const part = { + name: 'example_part', + draft: part => { + const { methodA, methodB } = part.shorthand() + methodA('hello A') + methodB('hello B') + } + } + const Test = new Design({plugins: [plugin], parts: [ part ]}) + const pattern = new Test() + pattern.draft() + expect(pattern.store.get("test.example_part.a")).to.equal("hello A") + expect(pattern.store.get("test.example_part.b")).to.equal("hello B") + }); });