diff --git a/CHANGELOG.md b/CHANGELOG.md
index f7da1ae..8f98971 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -2,6 +2,16 @@
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
+
+# [3.2.0](https://github.com/kisenka/svg-sprite-loader/compare/v3.1.7...v3.2.0) (2017-08-16)
+
+
+### Features
+
+* **runtime:** add ability to create sprite from existing DOM node ([4056d7b](https://github.com/kisenka/svg-sprite-loader/commit/4056d7b))
+
+
+
## [3.1.7](https://github.com/kisenka/svg-sprite-loader/compare/v3.1.6...v3.1.7) (2017-08-15)
diff --git a/examples/browser-sprite-with-dll/build/dll.js b/examples/browser-sprite-with-dll/build/dll.js
index 31aa54e..34d9ce3 100644
--- a/examples/browser-sprite-with-dll/build/dll.js
+++ b/examples/browser-sprite-with-dll/build/dll.js
@@ -1225,6 +1225,8 @@ var BrowserSprite = (function (Sprite$$1) {
* @return {Element|null} attached DOM Element. null if node to attach not found.
*/
BrowserSprite.prototype.attach = function attach (target) {
+ var this$1 = this;
+
var sprite = this;
if (sprite.isMounted) {
@@ -1233,15 +1235,23 @@ var BrowserSprite = (function (Sprite$$1) {
/** @type Element */
var node = typeof target === 'string' ? document.querySelector(target) : target;
+ sprite.node = node;
+ // Already added symbols needs to be mounted
+ this.symbols.forEach(function (symbol) {
+ symbol.mount(sprite.node);
+ this$1._emitter.emit(Events.SYMBOL_MOUNT, symbol.node);
+ });
+
+ // Create symbols from existing DOM nodes, add and mount them
arrayFrom(node.querySelectorAll('symbol'))
.forEach(function (symbolNode) {
var symbol = BrowserSpriteSymbol.createFromExistingNode(symbolNode);
- symbol.node = symbolNode;
+ symbol.node = symbolNode; // hack to prevent symbol mounting to sprite when adding
sprite.add(symbol);
});
- sprite.node = node;
+ this._emitter.emit(Events.MOUNT, node);
return node;
};
@@ -1366,21 +1376,33 @@ var ready$1 = createCommonjsModule(function (module) {
});
});
-var globaVarName = '__SVG_SPRITE__';
-var isSpriteExists = !!window[globaVarName];
+var spriteNodeId = '__SVG_SPRITE_NODE__';
+var spriteGlobalVarName = '__SVG_SPRITE__';
+var isSpriteExists = !!window[spriteGlobalVarName];
// eslint-disable-next-line import/no-mutable-exports
var sprite;
if (isSpriteExists) {
- sprite = window[globaVarName];
+ sprite = window[spriteGlobalVarName];
} else {
- sprite = new BrowserSprite();
- window[globaVarName] = sprite;
+ sprite = new BrowserSprite({ attrs: { id: spriteNodeId } });
+ window[spriteGlobalVarName] = sprite;
}
var loadSprite = function () {
- sprite.mount(document.body, true);
+ /**
+ * Check for page already contains sprite node
+ * If found - attach to and reuse it's content
+ * If not - render and mount the new sprite
+ */
+ var existing = document.getElementById(spriteNodeId);
+
+ if (existing) {
+ sprite.attach(existing);
+ } else {
+ sprite.mount(document.body, true);
+ }
};
if (document.body) {
diff --git a/examples/browser-sprite/build/main.js b/examples/browser-sprite/build/main.js
index b175f35..adb278a 100644
--- a/examples/browser-sprite/build/main.js
+++ b/examples/browser-sprite/build/main.js
@@ -1212,6 +1212,8 @@ var BrowserSprite = (function (Sprite$$1) {
* @return {Element|null} attached DOM Element. null if node to attach not found.
*/
BrowserSprite.prototype.attach = function attach (target) {
+ var this$1 = this;
+
var sprite = this;
if (sprite.isMounted) {
@@ -1220,15 +1222,23 @@ var BrowserSprite = (function (Sprite$$1) {
/** @type Element */
var node = typeof target === 'string' ? document.querySelector(target) : target;
+ sprite.node = node;
+ // Already added symbols needs to be mounted
+ this.symbols.forEach(function (symbol) {
+ symbol.mount(sprite.node);
+ this$1._emitter.emit(Events.SYMBOL_MOUNT, symbol.node);
+ });
+
+ // Create symbols from existing DOM nodes, add and mount them
arrayFrom(node.querySelectorAll('symbol'))
.forEach(function (symbolNode) {
var symbol = BrowserSpriteSymbol.createFromExistingNode(symbolNode);
- symbol.node = symbolNode;
+ symbol.node = symbolNode; // hack to prevent symbol mounting to sprite when adding
sprite.add(symbol);
});
- sprite.node = node;
+ this._emitter.emit(Events.MOUNT, node);
return node;
};
@@ -1353,21 +1363,33 @@ var ready$1 = createCommonjsModule(function (module) {
});
});
-var globaVarName = '__SVG_SPRITE__';
-var isSpriteExists = !!window[globaVarName];
+var spriteNodeId = '__SVG_SPRITE_NODE__';
+var spriteGlobalVarName = '__SVG_SPRITE__';
+var isSpriteExists = !!window[spriteGlobalVarName];
// eslint-disable-next-line import/no-mutable-exports
var sprite;
if (isSpriteExists) {
- sprite = window[globaVarName];
+ sprite = window[spriteGlobalVarName];
} else {
- sprite = new BrowserSprite();
- window[globaVarName] = sprite;
+ sprite = new BrowserSprite({ attrs: { id: spriteNodeId } });
+ window[spriteGlobalVarName] = sprite;
}
var loadSprite = function () {
- sprite.mount(document.body, true);
+ /**
+ * Check for page already contains sprite node
+ * If found - attach to and reuse it's content
+ * If not - render and mount the new sprite
+ */
+ var existing = document.getElementById(spriteNodeId);
+
+ if (existing) {
+ sprite.attach(existing);
+ } else {
+ sprite.mount(document.body, true);
+ }
};
if (document.body) {
diff --git a/examples/custom-runtime-generator/build/main.js b/examples/custom-runtime-generator/build/main.js
index 537955e..60b3537 100644
--- a/examples/custom-runtime-generator/build/main.js
+++ b/examples/custom-runtime-generator/build/main.js
@@ -10629,6 +10629,8 @@ var BrowserSprite = (function (Sprite$$1) {
* @return {Element|null} attached DOM Element. null if node to attach not found.
*/
BrowserSprite.prototype.attach = function attach (target) {
+ var this$1 = this;
+
var sprite = this;
if (sprite.isMounted) {
@@ -10637,15 +10639,23 @@ var BrowserSprite = (function (Sprite$$1) {
/** @type Element */
var node = typeof target === 'string' ? document.querySelector(target) : target;
+ sprite.node = node;
+ // Already added symbols needs to be mounted
+ this.symbols.forEach(function (symbol) {
+ symbol.mount(sprite.node);
+ this$1._emitter.emit(Events.SYMBOL_MOUNT, symbol.node);
+ });
+
+ // Create symbols from existing DOM nodes, add and mount them
arrayFrom(node.querySelectorAll('symbol'))
.forEach(function (symbolNode) {
var symbol = BrowserSpriteSymbol.createFromExistingNode(symbolNode);
- symbol.node = symbolNode;
+ symbol.node = symbolNode; // hack to prevent symbol mounting to sprite when adding
sprite.add(symbol);
});
- sprite.node = node;
+ this._emitter.emit(Events.MOUNT, node);
return node;
};
@@ -10770,21 +10780,33 @@ var ready$1 = createCommonjsModule(function (module) {
});
});
-var globaVarName = '__SVG_SPRITE__';
-var isSpriteExists = !!window[globaVarName];
+var spriteNodeId = '__SVG_SPRITE_NODE__';
+var spriteGlobalVarName = '__SVG_SPRITE__';
+var isSpriteExists = !!window[spriteGlobalVarName];
// eslint-disable-next-line import/no-mutable-exports
var sprite;
if (isSpriteExists) {
- sprite = window[globaVarName];
+ sprite = window[spriteGlobalVarName];
} else {
- sprite = new BrowserSprite();
- window[globaVarName] = sprite;
+ sprite = new BrowserSprite({ attrs: { id: spriteNodeId } });
+ window[spriteGlobalVarName] = sprite;
}
var loadSprite = function () {
- sprite.mount(document.body, true);
+ /**
+ * Check for page already contains sprite node
+ * If found - attach to and reuse it's content
+ * If not - render and mount the new sprite
+ */
+ var existing = document.getElementById(spriteNodeId);
+
+ if (existing) {
+ sprite.attach(existing);
+ } else {
+ sprite.mount(document.body, true);
+ }
};
if (document.body) {
diff --git a/examples/server-side-rendering/build/main.js b/examples/server-side-rendering/build/main.js
index 1a48fd9..5655da7 100644
--- a/examples/server-side-rendering/build/main.js
+++ b/examples/server-side-rendering/build/main.js
@@ -305,7 +305,7 @@ Sprite.prototype.destroy = function destroy () {
this.symbols.forEach(function (s) { return s.destroy(); });
};
-var sprite = new Sprite();
+var sprite = new Sprite({ attrs: { id: '__SVG_SPRITE_NODE__' } });
return sprite;
diff --git a/package.json b/package.json
index ed762e0..9f4dc8c 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
{
"name": "svg-sprite-loader",
- "version": "3.1.7",
+ "version": "3.2.0",
"description": "Webpack loader for creating SVG sprites",
"keywords": [
"svg",