Ticket #2708: infinite.html

File infinite.html, 6.5 KB (added by agiammarchi, 21 months ago)

infinite area sketch

Line 
1
2<!DOCTYPE html>
3<html>
4  <head>
5    <meta charset="utf-8">
6    <meta name="viewport" content="width=device-width,initial-scale=1">
7    <title>Infinite Scroll</title>
8    <script src="https://unpkg.com/hyperhtml"></script>
9    <script>
10    const easyList = fetch('https://easylist.to/easylist/easylist.txt').then(b => b.text());
11    addEventListener('DOMContentLoaded', () => {
12      easyList.then(text => {
13        const {wire} = hyperHTML;
14        const P = text => wire()`
15          <p
16            contenteditable
17            onkeypress=${blurOnEnter}
18            onblur=${saveOrRemove}
19            data-text=${text}
20          >${text}</p>`;
21        const blurOnEnter = event => {
22          if (event.key === 'Enter') {
23            event.preventDefault();
24            event.currentTarget.blur();
25          }
26        };
27        const indirectScroll = event => {
28          scrollTop = event.currentTarget.scrollTop;
29          update();
30        };
31        const saveOrRemove = event => {
32          const p = event.currentTarget;
33          const newText = p.textContent.trim();
34          const oldText = p.dataset.text;
35          if (newText !== oldText) {
36            const i = list.indexOf(p);
37            if (newText.length) {
38              rules[i] = newText;
39              p.dataset.text = newText;
40            } else {
41              list.splice(i, 1);
42              rules.splice(i, 1);
43            }
44            scrollContent.style.height = `${sampleHeight * (rules.length + 1)}px`;
45            scrollHeight = scrollContent.scrollHeight;
46            update();
47          }
48        };
49        const view = document.querySelector('.infinite');
50        const scroll = wire()`
51          <div class="infinite-scrollbar" onmouseup=${indirectScroll} />`;
52        const sample = view.appendChild(P('O'));
53        const sampleHeight = sample.offsetHeight;
54        let scrollTop = 0;
55        view.textContent = '';
56        view.addEventListener(
57          'wheel',
58          event => {
59            event.preventDefault();
60            scrollTop = Math.max(0, Math.min(scrollHeight, scrollTop + event.deltaY));
61            view.scrollLeft += event.deltaX;
62            update();
63          },
64          {passive: false}
65        );
66        view.parentNode.insertBefore(scroll, view.nextSibling);
67        const {top, left, width, height} = view.getBoundingClientRect();
68        scroll.style.cssText = [
69          `top:${top+window.scrollY}px`,
70          `left:${left+width+window.scrollX}px`,
71          `height:${height}px`
72        ].join(';');
73        const scrollContent = scroll.appendChild(wire()`<p />`);
74        const render = hyperHTML.bind(view);
75        const list = [];
76        const rules = text.trim().split('\n');
77        const colors = [];
78        scrollContent.style.height = `${sampleHeight * (rules.length + 1)}px`;
79        let scrollHeight = scrollContent.scrollHeight;
80        update();
81        function update() {
82          const newList = [];
83          const clientHeight = view.clientHeight;
84          let count = 0;
85          let i = Math.max(0, Math.floor(scrollTop / sampleHeight));
86          while ((count * sampleHeight) < (clientHeight + sampleHeight)) {
87            newList[count] = list[i] || (list[i] = P(rules[i] || ''));
88            count++;
89            i++;
90          }
91          render`${newList}`;
92          view.scrollTop = scrollTop % sampleHeight;
93          scroll.scrollTop = scrollTop;
94        }
95      }, {once: true});
96      });
97    </script>
98    <style>
99      html, body {
100        margin: 0;
101        padding: 0;
102        box-sizing: border-box;
103      }
104      div {
105        margin: auto;
106        height: 320px;
107        max-width: 320px;
108      }
109      .infinite {
110        padding: 0;
111        overflow-y: hidden;
112        box-sizing: border-box;
113        border: 1px solid silver;
114        border-right: 0;
115      }
116      .infinite > p {
117        margin: 0;
118        padding: 0 2px;
119        font-family: sans-serif;
120        white-space: nowrap;
121        box-sizing: inherit;
122      }
123      .infinite-scrollbar {
124        box-sizing: border-box;
125        overflow-y: auto;
126        position: absolute;
127        width: 16px;
128        border: 1px solid silver;
129        border-left: 0;
130      }
131      .infinite-scrollbar,
132      .infinite-scrollbar > p {
133        width: 16px;
134        padding: 0;
135        margin: 0;
136      }
137      body > p {
138        max-width: 640px;
139        margin: 32px auto;
140      }
141    </style>
142  </head>
143  <body>
144    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum</p>
145    <p>Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?</p>
146    <div class="infinite"></div>
147    <p>At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Temporibus autem quibusdam et aut officiis debitis aut rerum necessitatibus saepe eveniet ut et voluptates repudiandae sint et molestiae non recusandae. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.</p>
148  </body>
149</html>