@lvce-editor/completion-worker 1.12.0 → 1.14.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1145,7 +1145,7 @@ const isEqual$2 = (oldState, newState) => {
1145
1145
  };
1146
1146
 
1147
1147
  const isEqual$1 = (oldState, newState) => {
1148
- return oldState.items === newState.items && oldState.focusedIndex === newState.focusedIndex;
1148
+ return oldState.items === newState.items && oldState.focusedIndex === newState.focusedIndex && oldState.minLineY === newState.minLineY && oldState.maxLineY === newState.maxLineY;
1149
1149
  };
1150
1150
 
1151
1151
  const isEqual = (oldState, newState) => {
@@ -1761,6 +1761,28 @@ const getNumberOfVisibleItems = (listHeight, itemHeight) => {
1761
1761
  return Math.ceil(listHeight / itemHeight) + 1;
1762
1762
  };
1763
1763
 
1764
+ const getNewOffsets = (itemHeight, finalDeltaY, deltaY, height, headerHeight, value, itemCount) => {
1765
+ const listHeight = height - headerHeight;
1766
+ const newDeltaY = clamp(value, 0, finalDeltaY);
1767
+ if (deltaY === newDeltaY) {
1768
+ return {
1769
+ modified: false,
1770
+ newDeltaY: 0,
1771
+ newMinLineY: 0,
1772
+ newMaxLineY: 0
1773
+ };
1774
+ }
1775
+ // TODO when it only moves by one px, items don't need to be rerendered, only negative margin
1776
+ const minLineY = Math.floor(newDeltaY / itemHeight);
1777
+ const maxLineY = Math.min(minLineY + getNumberOfVisibleItems(listHeight, itemHeight), itemCount - 1);
1778
+ return {
1779
+ newDeltaY: newDeltaY,
1780
+ newMinLineY: minLineY,
1781
+ newMaxLineY: maxLineY,
1782
+ modified: true
1783
+ };
1784
+ };
1785
+
1764
1786
  const setDeltaY = (state, value) => {
1765
1787
  object(state);
1766
1788
  number(value);
@@ -1769,21 +1791,23 @@ const setDeltaY = (state, value) => {
1769
1791
  finalDeltaY,
1770
1792
  deltaY,
1771
1793
  height,
1772
- headerHeight
1794
+ headerHeight,
1795
+ items
1773
1796
  } = state;
1774
- const listHeight = height - headerHeight;
1775
- const newDeltaY = clamp(value, 0, finalDeltaY);
1776
- if (deltaY === newDeltaY) {
1797
+ const {
1798
+ newDeltaY,
1799
+ newMaxLineY,
1800
+ newMinLineY,
1801
+ modified
1802
+ } = getNewOffsets(itemHeight, finalDeltaY, deltaY, height, headerHeight, value, items.length);
1803
+ if (!modified) {
1777
1804
  return state;
1778
1805
  }
1779
- // TODO when it only moves by one px, extensions don't need to be rerendered, only negative margin
1780
- const minLineY = Math.floor(newDeltaY / itemHeight);
1781
- const maxLineY = minLineY + getNumberOfVisibleItems(listHeight, itemHeight);
1782
1806
  return {
1783
1807
  ...state,
1784
1808
  deltaY: newDeltaY,
1785
- minLineY,
1786
- maxLineY
1809
+ minLineY: newMinLineY,
1810
+ maxLineY: newMaxLineY
1787
1811
  };
1788
1812
  };
1789
1813
 
@@ -1911,7 +1935,8 @@ const HandleWheel = 'handleWheel';
1911
1935
  const getEventListeners = () => {
1912
1936
  return [{
1913
1937
  name: HandleWheel,
1914
- params: ['EditorCompletion.handleWheel', 'event.deltaMode', 'event.deltaY']
1938
+ params: ['EditorCompletion.handleWheel', 'event.deltaMode', 'event.deltaY'],
1939
+ passive: true
1915
1940
  }];
1916
1941
  };
1917
1942
 
@@ -1939,8 +1964,11 @@ const EditorCompletionItemFocused = 'EditorCompletionItemFocused';
1939
1964
  const EditorCompletionItemHighlight = 'EditorCompletionItemHighlight';
1940
1965
  const FileIcon = 'FileIcon';
1941
1966
  const Label = 'Label';
1942
- const Viewlet = 'Viewlet';
1943
1967
  const ListItems = 'ListItems';
1968
+ const ScrollBar = 'ScrollBar';
1969
+ const ScrollBarSmall = 'ScrollBarSmall';
1970
+ const ScrollBarThumb = 'ScrollBarThumb';
1971
+ const Viewlet = 'Viewlet';
1944
1972
 
1945
1973
  const emptyObject = {};
1946
1974
  const RE_PLACEHOLDER = /\{(PH\d+)\}/g;
@@ -2000,6 +2028,12 @@ const text = data => {
2000
2028
  childCount: 0
2001
2029
  };
2002
2030
  };
2031
+ const px = value => {
2032
+ return `${value}px`;
2033
+ };
2034
+ const position = (x, y) => {
2035
+ return `${x}px ${y}px`;
2036
+ };
2003
2037
 
2004
2038
  const label1 = {
2005
2039
  type: Div,
@@ -2090,12 +2124,36 @@ const getCompletionItemsVirtualDom = visibleItems => {
2090
2124
  return dom;
2091
2125
  };
2092
2126
 
2093
- const getCompletionVirtualDom = visibleItems => {
2127
+ const getScrollBarVirtualDom = (scrollBarHeight, scrollBarTop) => {
2128
+ const shouldShowScrollbar = scrollBarHeight > 0;
2129
+ if (!shouldShowScrollbar) {
2130
+ return [];
2131
+ }
2132
+ const heightString = px(scrollBarHeight);
2133
+ const translateString = position(0, scrollBarTop);
2094
2134
  return [{
2095
2135
  type: Div,
2096
- className: mergeClassNames(Viewlet, EditorCompletion),
2097
- id: 'Completions',
2136
+ className: mergeClassNames(ScrollBar, ScrollBarSmall),
2098
2137
  childCount: 1
2138
+ }, {
2139
+ type: Div,
2140
+ className: ScrollBarThumb,
2141
+ childCount: 0,
2142
+ height: heightString,
2143
+ translate: translateString
2144
+ }];
2145
+ };
2146
+
2147
+ const Completions = 'Completions';
2148
+
2149
+ const getCompletionVirtualDom = (visibleItems, scrollBarHeight, scrollBarTop) => {
2150
+ const scrollBarDom = getScrollBarVirtualDom(scrollBarHeight, scrollBarTop);
2151
+ const childCount = scrollBarDom.length === 0 ? 1 : 2;
2152
+ return [{
2153
+ type: Div,
2154
+ className: mergeClassNames(Viewlet, EditorCompletion),
2155
+ id: Completions,
2156
+ childCount: childCount
2099
2157
  }, {
2100
2158
  type: Div,
2101
2159
  className: ListItems,
@@ -2103,12 +2161,23 @@ const getCompletionVirtualDom = visibleItems => {
2103
2161
  ariaLabel: suggest(),
2104
2162
  childCount: 1,
2105
2163
  onWheel: HandleWheel
2106
- }, ...getCompletionItemsVirtualDom(visibleItems)
2107
-
2164
+ }, ...getCompletionItemsVirtualDom(visibleItems), ...scrollBarDom
2108
2165
  // TODO render scrollbar
2109
2166
  ];
2110
2167
  };
2111
2168
 
2169
+ const getScrollBarSize = (size, contentSize, minimumSliderSize) => {
2170
+ if (size >= contentSize) {
2171
+ return 0;
2172
+ }
2173
+ return Math.max(Math.round(size ** 2 / contentSize), minimumSliderSize);
2174
+ };
2175
+
2176
+ const getScrollBarTop = (height, contentHeight, scrollTop) => {
2177
+ const scrollBarTop = Math.round(scrollTop / contentHeight * height);
2178
+ return scrollBarTop;
2179
+ };
2180
+
2112
2181
  const Property = 1;
2113
2182
  const Value = 2;
2114
2183
  const Function = 3;
@@ -2171,11 +2240,15 @@ const getLabel = item => {
2171
2240
  return item.label;
2172
2241
  };
2173
2242
 
2174
- const getVisibleIem = (item, itemHeight, leadingWord, i, focusedIndex) => {
2243
+ const getTop = (i, minLineY, itemHeight, relative) => {
2244
+ return (i - minLineY) * itemHeight - relative;
2245
+ };
2246
+
2247
+ const getVisibleIem = (item, itemHeight, leadingWord, i, minLineY, focusedIndex, relative) => {
2175
2248
  return {
2176
2249
  label: getLabel(item),
2177
2250
  symbolName: getSymbolName(item.kind),
2178
- top: i * itemHeight,
2251
+ top: getTop(i, minLineY, itemHeight, relative),
2179
2252
  highlights: getHighlights(item),
2180
2253
  focused: i === focusedIndex,
2181
2254
  deprecated: item.flags & Deprecated,
@@ -2183,21 +2256,27 @@ const getVisibleIem = (item, itemHeight, leadingWord, i, focusedIndex) => {
2183
2256
  };
2184
2257
  };
2185
2258
 
2186
- const getVisibleItems = (filteredItems, itemHeight, leadingWord, minLineY, maxLineY, focusedIndex) => {
2259
+ const getVisibleItems = (filteredItems, itemHeight, leadingWord, minLineY, maxLineY, focusedIndex, deltaY) => {
2187
2260
  const visibleItems = [];
2261
+ const relative = deltaY % itemHeight;
2188
2262
  for (let i = minLineY; i < maxLineY; i++) {
2189
2263
  const filteredItem = filteredItems[i];
2190
- visibleItems.push(getVisibleIem(filteredItem, itemHeight, leadingWord, i, focusedIndex));
2264
+ visibleItems.push(getVisibleIem(filteredItem, itemHeight, leadingWord, i, minLineY, focusedIndex, relative));
2191
2265
  }
2192
2266
  return visibleItems;
2193
2267
  };
2194
2268
 
2195
2269
  const renderItems = (oldState, newState) => {
2196
2270
  const {
2197
- uid
2271
+ uid,
2272
+ height,
2273
+ deltaY
2198
2274
  } = newState;
2199
- const visibleItems = getVisibleItems(newState.items, newState.itemHeight, newState.leadingWord, newState.minLineY, newState.maxLineY, newState.focusedIndex);
2200
- const dom = getCompletionVirtualDom(visibleItems);
2275
+ const visibleItems = getVisibleItems(newState.items, newState.itemHeight, newState.leadingWord, newState.minLineY, newState.maxLineY, newState.focusedIndex, newState.deltaY);
2276
+ const contentHeight = newState.items.length * newState.itemHeight;
2277
+ const scrollBarHeight = getScrollBarSize(height, contentHeight, 20);
2278
+ const scrollBarTop = getScrollBarTop(height, contentHeight, deltaY);
2279
+ const dom = getCompletionVirtualDom(visibleItems, scrollBarHeight, scrollBarTop);
2201
2280
  return [SetDom2, uid, dom];
2202
2281
  };
2203
2282
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/completion-worker",
3
- "version": "1.12.0",
3
+ "version": "1.14.0",
4
4
  "description": "Web Worker for the find widget in Lvce Editor",
5
5
  "repository": {
6
6
  "type": "git",