attachto.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. function pre(vnode, newVnode) {
  2. const attachData = vnode.data.attachData;
  3. // Copy created placeholder and real element from old vnode
  4. newVnode.data.attachData.placeholder = attachData.placeholder;
  5. newVnode.data.attachData.real = attachData.real;
  6. // Mount real element in vnode so the patch process operates on it
  7. vnode.elm = vnode.data.attachData.real;
  8. }
  9. function post(_, vnode) {
  10. // Mount dummy placeholder in vnode so potential reorders use it
  11. vnode.elm = vnode.data.attachData.placeholder;
  12. }
  13. function destroy(vnode) {
  14. // Remove placeholder
  15. if (vnode.elm !== undefined) {
  16. vnode.elm.parentNode.removeChild(vnode.elm);
  17. }
  18. // Remove real element from where it was inserted
  19. vnode.elm = vnode.data.attachData.real;
  20. }
  21. function create(_, vnode) {
  22. const real = vnode.elm;
  23. const attachData = vnode.data.attachData;
  24. const placeholder = document.createElement("span");
  25. // Replace actual element with dummy placeholder
  26. // Snabbdom will then insert placeholder instead
  27. vnode.elm = placeholder;
  28. attachData.target.appendChild(real);
  29. attachData.real = real;
  30. attachData.placeholder = placeholder;
  31. }
  32. export function attachTo(target, vnode) {
  33. if (vnode.data === undefined)
  34. vnode.data = {};
  35. if (vnode.data.hook === undefined)
  36. vnode.data.hook = {};
  37. const data = vnode.data;
  38. const hook = vnode.data.hook;
  39. data.attachData = { target: target, placeholder: undefined, real: undefined };
  40. hook.create = create;
  41. hook.prepatch = pre;
  42. hook.postpatch = post;
  43. hook.destroy = destroy;
  44. return vnode;
  45. }
  46. //# sourceMappingURL=attachto.js.map