From cbfc92cdd5652dbcfa38eb33762f1c25e63e6131 Mon Sep 17 00:00:00 2001 From: surunzi Date: Tue, 9 Jan 2018 19:23:02 +0800 Subject: [PATCH] Dev: Expose snippets run api --- doc/TOOL_API.md | 9 +++++++++ src/Snippets/Snippets.js | 9 +++++++++ 2 files changed, 18 insertions(+) diff --git a/doc/TOOL_API.md b/doc/TOOL_API.md index 090a4b3..d37fc72 100644 --- a/doc/TOOL_API.md +++ b/doc/TOOL_API.md @@ -189,12 +189,21 @@ Remove specified snippet. |----|------|-----------------| |name|string|Snippet to remove| +### run + +Run specified snippet. + +|Name|Type |Desc | +|----|------|--------------| +|name|string|Snippet to run| + ```javascript snippets.add('hello', function () { console.log('Hello World!'); }, 'Display hello on console'); +snippets.run('hello'); snippets.remove('hello'); ``` diff --git a/src/Snippets/Snippets.js b/src/Snippets/Snippets.js index 3713460..7b6ceb6 100644 --- a/src/Snippets/Snippets.js +++ b/src/Snippets/Snippets.js @@ -49,6 +49,15 @@ export default class Snippets extends Tool return this; } + run(name) + { + let snippets = this._snippets; + + for (let i = 0, len = snippets.length; i < len; i++) + { + if (snippets[i].name === name) this._run(i); + } + } clear() { this._snippets = [];