UNPKG

svelte-icomoon

Version:

It makes it very simple to use SVG icons in your Svelte projects.

703 lines (654 loc) 22.6 kB
(function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : typeof define === 'function' && define.amd ? define(factory) : (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.Icomoon = factory()); })(this, (function () { 'use strict'; function noop() { } function assign(tar, src) { // @ts-ignore for (const k in src) tar[k] = src[k]; return tar; } function run(fn) { return fn(); } function blank_object() { return Object.create(null); } function run_all(fns) { fns.forEach(run); } function is_function(thing) { return typeof thing === 'function'; } function safe_not_equal(a, b) { return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); } function is_empty(obj) { return Object.keys(obj).length === 0; } function exclude_internal_props(props) { const result = {}; for (const k in props) if (k[0] !== '$') result[k] = props[k]; return result; } // Track which nodes are claimed during hydration. Unclaimed nodes can then be removed from the DOM // at the end of hydration without touching the remaining nodes. let is_hydrating = false; function start_hydrating() { is_hydrating = true; } function end_hydrating() { is_hydrating = false; } function upper_bound(low, high, key, value) { // Return first index of value larger than input value in the range [low, high) while (low < high) { const mid = low + ((high - low) >> 1); if (key(mid) <= value) { low = mid + 1; } else { high = mid; } } return low; } function init_hydrate(target) { if (target.hydrate_init) return; target.hydrate_init = true; // We know that all children have claim_order values since the unclaimed have been detached const children = target.childNodes; /* * Reorder claimed children optimally. * We can reorder claimed children optimally by finding the longest subsequence of * nodes that are already claimed in order and only moving the rest. The longest * subsequence subsequence of nodes that are claimed in order can be found by * computing the longest increasing subsequence of .claim_order values. * * This algorithm is optimal in generating the least amount of reorder operations * possible. * * Proof: * We know that, given a set of reordering operations, the nodes that do not move * always form an increasing subsequence, since they do not move among each other * meaning that they must be already ordered among each other. Thus, the maximal * set of nodes that do not move form a longest increasing subsequence. */ // Compute longest increasing subsequence // m: subsequence length j => index k of smallest value that ends an increasing subsequence of length j const m = new Int32Array(children.length + 1); // Predecessor indices + 1 const p = new Int32Array(children.length); m[0] = -1; let longest = 0; for (let i = 0; i < children.length; i++) { const current = children[i].claim_order; // Find the largest subsequence length such that it ends in a value less than our current value // upper_bound returns first greater value, so we subtract one const seqLen = upper_bound(1, longest + 1, idx => children[m[idx]].claim_order, current) - 1; p[i] = m[seqLen] + 1; const newLen = seqLen + 1; // We can guarantee that current is the smallest value. Otherwise, we would have generated a longer sequence. m[newLen] = i; longest = Math.max(newLen, longest); } // The longest increasing subsequence of nodes (initially reversed) const lis = []; // The rest of the nodes, nodes that will be moved const toMove = []; let last = children.length - 1; for (let cur = m[longest] + 1; cur != 0; cur = p[cur - 1]) { lis.push(children[cur - 1]); for (; last >= cur; last--) { toMove.push(children[last]); } last--; } for (; last >= 0; last--) { toMove.push(children[last]); } lis.reverse(); // We sort the nodes being moved to guarantee that their insertion order matches the claim order toMove.sort((a, b) => a.claim_order - b.claim_order); // Finally, we move the nodes for (let i = 0, j = 0; i < toMove.length; i++) { while (j < lis.length && toMove[i].claim_order >= lis[j].claim_order) { j++; } const anchor = j < lis.length ? lis[j] : null; target.insertBefore(toMove[i], anchor); } } function append(target, node) { if (is_hydrating) { init_hydrate(target); if ((target.actual_end_child === undefined) || ((target.actual_end_child !== null) && (target.actual_end_child.parentElement !== target))) { target.actual_end_child = target.firstChild; } if (node !== target.actual_end_child) { target.insertBefore(node, target.actual_end_child); } else { target.actual_end_child = node.nextSibling; } } else if (node.parentNode !== target) { target.appendChild(node); } } function insert(target, node, anchor) { if (is_hydrating && !anchor) { append(target, node); } else if (node.parentNode !== target || node.nextSibling != anchor) { target.insertBefore(node, anchor || null); } } function detach(node) { node.parentNode.removeChild(node); } function destroy_each(iterations, detaching) { for (let i = 0; i < iterations.length; i += 1) { if (iterations[i]) iterations[i].d(detaching); } } function svg_element(name) { return document.createElementNS('http://www.w3.org/2000/svg', name); } function text(data) { return document.createTextNode(data); } function empty() { return text(''); } function attr(node, attribute, value) { if (value == null) node.removeAttribute(attribute); else if (node.getAttribute(attribute) !== value) node.setAttribute(attribute, value); } function set_svg_attributes(node, attributes) { for (const key in attributes) { attr(node, key, attributes[key]); } } function children(element) { return Array.from(element.childNodes); } function set_data(text, data) { data = '' + data; if (text.wholeText !== data) text.data = data; } let current_component; function set_current_component(component) { current_component = component; } const dirty_components = []; const binding_callbacks = []; const render_callbacks = []; const flush_callbacks = []; const resolved_promise = Promise.resolve(); let update_scheduled = false; function schedule_update() { if (!update_scheduled) { update_scheduled = true; resolved_promise.then(flush); } } function add_render_callback(fn) { render_callbacks.push(fn); } let flushing = false; const seen_callbacks = new Set(); function flush() { if (flushing) return; flushing = true; do { // first, call beforeUpdate functions // and update components for (let i = 0; i < dirty_components.length; i += 1) { const component = dirty_components[i]; set_current_component(component); update(component.$$); } set_current_component(null); dirty_components.length = 0; while (binding_callbacks.length) binding_callbacks.pop()(); // then, once components are updated, call // afterUpdate functions. This may cause // subsequent updates... for (let i = 0; i < render_callbacks.length; i += 1) { const callback = render_callbacks[i]; if (!seen_callbacks.has(callback)) { // ...so guard against infinite loops seen_callbacks.add(callback); callback(); } } render_callbacks.length = 0; } while (dirty_components.length); while (flush_callbacks.length) { flush_callbacks.pop()(); } update_scheduled = false; flushing = false; seen_callbacks.clear(); } function update($$) { if ($$.fragment !== null) { $$.update(); run_all($$.before_update); const dirty = $$.dirty; $$.dirty = [-1]; $$.fragment && $$.fragment.p($$.ctx, dirty); $$.after_update.forEach(add_render_callback); } } const outroing = new Set(); function transition_in(block, local) { if (block && block.i) { outroing.delete(block); block.i(local); } } function get_spread_update(levels, updates) { const update = {}; const to_null_out = {}; const accounted_for = { $$scope: 1 }; let i = levels.length; while (i--) { const o = levels[i]; const n = updates[i]; if (n) { for (const key in o) { if (!(key in n)) to_null_out[key] = 1; } for (const key in n) { if (!accounted_for[key]) { update[key] = n[key]; accounted_for[key] = 1; } } levels[i] = n; } else { for (const key in o) { accounted_for[key] = 1; } } } for (const key in to_null_out) { if (!(key in update)) update[key] = undefined; } return update; } function mount_component(component, target, anchor, customElement) { const { fragment, on_mount, on_destroy, after_update } = component.$$; fragment && fragment.m(target, anchor); if (!customElement) { // onMount happens before the initial afterUpdate add_render_callback(() => { const new_on_destroy = on_mount.map(run).filter(is_function); if (on_destroy) { on_destroy.push(...new_on_destroy); } else { // Edge case - component was destroyed immediately, // most likely as a result of a binding initialising run_all(new_on_destroy); } component.$$.on_mount = []; }); } after_update.forEach(add_render_callback); } function destroy_component(component, detaching) { const $$ = component.$$; if ($$.fragment !== null) { run_all($$.on_destroy); $$.fragment && $$.fragment.d(detaching); // TODO null out other refs, including component.$$ (but need to // preserve final state?) $$.on_destroy = $$.fragment = null; $$.ctx = []; } } function make_dirty(component, i) { if (component.$$.dirty[0] === -1) { dirty_components.push(component); schedule_update(); component.$$.dirty.fill(0); } component.$$.dirty[(i / 31) | 0] |= (1 << (i % 31)); } function init(component, options, instance, create_fragment, not_equal, props, dirty = [-1]) { const parent_component = current_component; set_current_component(component); const $$ = component.$$ = { fragment: null, ctx: null, // state props, update: noop, not_equal, bound: blank_object(), // lifecycle on_mount: [], on_destroy: [], on_disconnect: [], before_update: [], after_update: [], context: new Map(parent_component ? parent_component.$$.context : options.context || []), // everything else callbacks: blank_object(), dirty, skip_bound: false }; let ready = false; $$.ctx = instance ? instance(component, options.props || {}, (i, ret, ...rest) => { const value = rest.length ? rest[0] : ret; if ($$.ctx && not_equal($$.ctx[i], $$.ctx[i] = value)) { if (!$$.skip_bound && $$.bound[i]) $$.bound[i](value); if (ready) make_dirty(component, i); } return ret; }) : []; $$.update(); ready = true; run_all($$.before_update); // `false` as a special case of no DOM component $$.fragment = create_fragment ? create_fragment($$.ctx) : false; if (options.target) { if (options.hydrate) { start_hydrating(); const nodes = children(options.target); // eslint-disable-next-line @typescript-eslint/no-non-null-assertion $$.fragment && $$.fragment.l(nodes); nodes.forEach(detach); } else { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion $$.fragment && $$.fragment.c(); } if (options.intro) transition_in(component.$$.fragment); mount_component(component, options.target, options.anchor, options.customElement); end_hydrating(); flush(); } set_current_component(parent_component); } /** * Base class for Svelte components. Used when dev=false. */ class SvelteComponent { $destroy() { destroy_component(this, 1); this.$destroy = noop; } $on(type, callback) { const callbacks = (this.$$.callbacks[type] || (this.$$.callbacks[type] = [])); callbacks.push(callback); return () => { const index = callbacks.indexOf(callback); if (index !== -1) callbacks.splice(index, 1); }; } $set($$props) { if (this.$$set && !is_empty($$props)) { this.$$.skip_bound = true; this.$$set($$props); this.$$.skip_bound = false; } } } const toInlineStyle = (obj = {}) => { let inlineStyle = ""; Object.keys(obj).forEach((capitalName) => { let value = obj[capitalName]; const name = toCebabCase(capitalName); if (typeof value === "number" && value !== 0) { value = value + "px"; } else if (Array.isArray(value)) { value = value .map((val) => { if (typeof val === "number" && val !== 0) { return val + "px"; } return val; }) .join(" "); } inlineStyle += `${name}:${value};`; }); return inlineStyle; }; const toCebabCase = (string) => string .replace(/([a-z])([A-Z])/g, "$1-$2") .replace(/\s+/g, "-") .toLowerCase(); /* src/Icomoon.svelte generated by Svelte v3.39.0 */ function get_each_context(ctx, list, i) { const child_ctx = ctx.slice(); child_ctx[15] = list[i]; return child_ctx; } // (51:1) {#each paths as path} function create_each_block(ctx) { let path; return { c() { path = svg_element("path"); attr(path, "d", /*path*/ ctx[15].d); }, m(target, anchor) { insert(target, path, anchor); }, p: noop, d(detaching) { if (detaching) detach(path); } }; } // (54:1) {#if title} function create_if_block(ctx) { let title_1; let t; return { c() { title_1 = svg_element("title"); t = text(/*title*/ ctx[0]); }, m(target, anchor) { insert(target, title_1, anchor); append(title_1, t); }, p(ctx, dirty) { if (dirty & /*title*/ 1) set_data(t, /*title*/ ctx[0]); }, d(detaching) { if (detaching) detach(title_1); } }; } function create_fragment(ctx) { let svg; let each_1_anchor; let svg_style_value; let each_value = /*paths*/ ctx[3]; let each_blocks = []; for (let i = 0; i < each_value.length; i += 1) { each_blocks[i] = create_each_block(get_each_context(ctx, each_value, i)); } let if_block = /*title*/ ctx[0] && create_if_block(ctx); let svg_levels = [ { viewBox: /*viewBox*/ ctx[2] }, { style: svg_style_value = toInlineStyle(/*_style*/ ctx[1]) }, /*$$props*/ ctx[4] ]; let svg_data = {}; for (let i = 0; i < svg_levels.length; i += 1) { svg_data = assign(svg_data, svg_levels[i]); } return { c() { svg = svg_element("svg"); for (let i = 0; i < each_blocks.length; i += 1) { each_blocks[i].c(); } each_1_anchor = empty(); if (if_block) if_block.c(); set_svg_attributes(svg, svg_data); }, m(target, anchor) { insert(target, svg, anchor); for (let i = 0; i < each_blocks.length; i += 1) { each_blocks[i].m(svg, null); } append(svg, each_1_anchor); if (if_block) if_block.m(svg, null); }, p(ctx, [dirty]) { if (dirty & /*paths*/ 8) { each_value = /*paths*/ ctx[3]; let i; for (i = 0; i < each_value.length; i += 1) { const child_ctx = get_each_context(ctx, each_value, i); if (each_blocks[i]) { each_blocks[i].p(child_ctx, dirty); } else { each_blocks[i] = create_each_block(child_ctx); each_blocks[i].c(); each_blocks[i].m(svg, each_1_anchor); } } for (; i < each_blocks.length; i += 1) { each_blocks[i].d(1); } each_blocks.length = each_value.length; } if (/*title*/ ctx[0]) { if (if_block) { if_block.p(ctx, dirty); } else { if_block = create_if_block(ctx); if_block.c(); if_block.m(svg, null); } } else if (if_block) { if_block.d(1); if_block = null; } set_svg_attributes(svg, svg_data = get_spread_update(svg_levels, [ { viewBox: /*viewBox*/ ctx[2] }, dirty & /*_style*/ 2 && svg_style_value !== (svg_style_value = toInlineStyle(/*_style*/ ctx[1])) && { style: svg_style_value }, dirty & /*$$props*/ 16 && /*$$props*/ ctx[4] ])); }, i: noop, o: noop, d(detaching) { if (detaching) detach(svg); destroy_each(each_blocks, detaching); if (if_block) if_block.d(); } }; } function instance($$self, $$props, $$invalidate) { let { iconSet } = $$props; let { name } = $$props; let { title } = $$props; let { color } = $$props; let { size = 16 } = $$props; let { disableFill = false } = $$props; let { removeInitialStyle = false } = $$props; let { style = {} } = $$props; const initialStyle = { display: "inline-block", stroke: "currentColor", fill: "currentColor" }; const currentIcon = iconSet.icons.find(item => item.properties.name === name); const { width = "1024" } = currentIcon?.icon || {}; const viewBox = `0 0 ${width} 1024`; const _style = { ...removeInitialStyle ? {} : initialStyle, ...style }; if (size) { _style.width = size; _style.height = size; } if (color) { _style.color = color; } const paths = currentIcon?.icon.paths.map((path, index) => { const pathProps = { d: path, ...!disableFill ? currentIcon?.icon.attrs[index] : {} }; return pathProps; }) || []; $$self.$$set = $$new_props => { $$invalidate(4, $$props = assign(assign({}, $$props), exclude_internal_props($$new_props))); if ('iconSet' in $$new_props) $$invalidate(5, iconSet = $$new_props.iconSet); if ('name' in $$new_props) $$invalidate(6, name = $$new_props.name); if ('title' in $$new_props) $$invalidate(0, title = $$new_props.title); if ('color' in $$new_props) $$invalidate(7, color = $$new_props.color); if ('size' in $$new_props) $$invalidate(8, size = $$new_props.size); if ('disableFill' in $$new_props) $$invalidate(9, disableFill = $$new_props.disableFill); if ('removeInitialStyle' in $$new_props) $$invalidate(10, removeInitialStyle = $$new_props.removeInitialStyle); if ('style' in $$new_props) $$invalidate(11, style = $$new_props.style); }; $$props = exclude_internal_props($$props); return [ title, _style, viewBox, paths, $$props, iconSet, name, color, size, disableFill, removeInitialStyle, style ]; } class Icomoon extends SvelteComponent { constructor(options) { super(); init(this, options, instance, create_fragment, safe_not_equal, { iconSet: 5, name: 6, title: 0, color: 7, size: 8, disableFill: 9, removeInitialStyle: 10, style: 11 }); } } return Icomoon; }));