UNPKG

@popeindustries/lit-html

Version:

Seamlessly and efficiently use @popeindustries/lit-html-server rendered HTML to hydrate lit-html templates in the browser

6 lines (5 loc) 1.69 kB
/** * @license * Copyright 2017 Google LLC * SPDX-License-Identifier: BSD-3-Clause */import{noChange as A}from"../lit-html.js";import{directive as C,Directive as M,PartType as P}from"../directive.js";import{insertPart as m,getCommittedValue as D,removePart as g,setCommittedValue as H,setChildPartValue as h}from"../directive-helpers.js";const V=(v,l,f)=>{const d=new Map;for(let u=l;u<=f;u++)d.set(v[u],u);return d};class I extends M{constructor(l){if(super(l),l.type!==P.CHILD)throw new Error("repeat() can only be used in text expressions")}_getValuesAndKeys(l,f,d){let u;d===void 0?d=f:f!==void 0&&(u=f);const w=[],e=[];let n=0;for(const r of l)w[n]=u?u(r,n):n,e[n]=d(r,n),n++;return{values:e,keys:w}}render(l,f,d){return this._getValuesAndKeys(l,f,d).values}update(l,[f,d,u]){var w;const e=D(l),{values:n,keys:r}=this._getValuesAndKeys(f,d,u);if(!Array.isArray(e))return this._itemKeys=r,n;const c=(w=this._itemKeys)!==null&&w!==void 0?w:this._itemKeys=[],a=[];let p,x,s=0,i=e.length-1,t=0,o=n.length-1;for(;s<=i&&t<=o;)if(e[s]===null)s++;else if(e[i]===null)i--;else if(c[s]===r[t])a[t]=h(e[s],n[t]),s++,t++;else if(c[i]===r[o])a[o]=h(e[i],n[o]),i--,o--;else if(c[s]===r[o])a[o]=h(e[s],n[o]),m(l,a[o+1],e[s]),s++,o--;else if(c[i]===r[t])a[t]=h(e[i],n[t]),m(l,e[s],e[i]),i--,t++;else if(p===void 0&&(p=V(r,t,o),x=V(c,s,i)),!p.has(c[s]))g(e[s]),s++;else if(!p.has(c[i]))g(e[i]),i--;else{const y=x.get(r[t]),K=y!==void 0?e[y]:null;if(K===null){const _=m(l,e[s]);h(_,n[t]),a[t]=_}else a[t]=h(K,n[t]),m(l,e[s],K),e[y]=null;t++}for(;t<=o;){const y=m(l,a[o+1]);h(y,n[t]),a[t++]=y}for(;s<=i;){const y=e[s++];y!==null&&g(y)}return this._itemKeys=r,H(l,a),A}}const E=C(I);export{E as repeat};