@singapore-editor/gutters 0.1.1 → 0.2.11

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.
package/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Shaul Lavo
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
package/README.md CHANGED
@@ -1,6 +1,8 @@
1
1
  # @singapore-editor/gutters
2
2
 
3
- Line-number and fold-gutter plugins for `@singapore-editor/core`.
3
+ Line numbers and fold controls for the Singapore editor.
4
+
5
+ Part of [Singapore](https://singapore.shaulavo.dev/). A code editor for the browser that keeps every version.
4
6
 
5
7
  ## Install
6
8
 
@@ -13,36 +15,43 @@ npm install @singapore-editor/core @singapore-editor/gutters
13
15
  ```ts
14
16
  import { Editor } from '@singapore-editor/core/editor'
15
17
  import { createFoldGutterPlugin, createLineGutterPlugin } from '@singapore-editor/gutters'
16
- import '@singapore-editor/core/style.css'
17
- import '@singapore-editor/gutters/style.css'
18
18
 
19
- const editor = new Editor(document.querySelector('#editor')!, {
19
+ const host = document.createElement('div')
20
+ host.style.height = '400px'
21
+ document.body.append(host)
22
+
23
+ const editor = new Editor(host, {
20
24
  plugins: [createLineGutterPlugin(), createFoldGutterPlugin()],
21
25
  })
26
+ editor.setText('const value = 1\n')
22
27
  ```
23
28
 
24
- Fold icons accept strings, DOM factories, or a `FoldGutterSvgIcon` descriptor through `icon`,
25
- `expandedIcon`, and `collapsedIcon`:
29
+ ## API highlights
26
30
 
27
- ```ts
28
- createFoldGutterPlugin({
29
- icon: { kind: 'svg', viewBox: '0 0 16 16', path: 'M2 5L8 11L14 5Z' },
30
- iconClassName: 'fold-icon',
31
- })
32
- ```
31
+ - `createLineGutterPlugin()` adds line numbers.
32
+ - `createFoldGutterPlugin()` adds fold controls.
33
+
34
+ [Generated API reference](https://singapore.shaulavo.dev/docs/reference/api/gutters/overview/)
35
+
36
+ ## Scroll gutters with the text
37
+
38
+ Gutters stay at the viewport's left edge by default. Set the editor's `gutterScroll` option to
39
+ `'content'` to move line numbers, fold arrows and other gutter lanes with horizontal text scrolling.
40
+ This is useful for code blocks in documentation and other reading views.
41
+
42
+ Pass `gutterScroll: 'content'` alongside the editor's `plugins` option. Call
43
+ `editor.setGutterScroll('fixed')` to switch an open editor back to fixed gutters.
44
+
45
+ The option applies to every gutter lane, including `gutterLeadingInset`. Vertical scrolling keeps
46
+ lanes aligned with their rows. Word wrap keeps its usual text width and continuation-row labels.
47
+ Use `'fixed'` to keep gutters at the left edge.
48
+
49
+ ## In the Singapore family
50
+
51
+ `@singapore-editor/core` owns the editor view. Add these plugins for line numbers and fold controls.
52
+
53
+ [Singapore README](https://github.com/ShaulLavo/singapore/blob/main/README.md) · [Documentation](https://singapore.shaulavo.dev/docs/start-here/introduction/)
54
+
55
+ ## License
33
56
 
34
- SVG icons use `currentColor` and fill their wrapper. Set its dimensions through `iconClassName`;
35
- the parent button's `data-editor-fold-state` is `expanded` or `collapsed` for state styling.
36
- Strings and SVG descriptors support provisional paint snapshots. DOM factories disable fold-gutter
37
- snapshots because restoring them would require the source fold marker.
38
-
39
- ## Exports
40
-
41
- - `createLineGutterPlugin` adds a line-number gutter.
42
- - `createFoldGutterPlugin` adds fold controls for syntax fold markers.
43
- - `createLineGutterContribution` and `createFoldGutterContribution` expose the lower-level gutter
44
- contributions.
45
- - The gutter passes presses through to the text. A contribution whose cell handles them sets
46
- `interactive: true`, as the fold gutter does; its cell then takes pointer events, and a press it
47
- does not claim still places the caret.
48
- - `@singapore-editor/gutters/style.css` imports both gutter styles.
57
+ MIT. [License](https://github.com/ShaulLavo/singapore/blob/main/LICENSE)
@@ -1,6 +1,6 @@
1
1
  import { addClassName, normalizeNonNegativeNumber, setElementHidden } from "./utils.js";
2
2
  import "./foldGutter.css";
3
- //#region src/foldGutter.ts
3
+ // src/foldGutter.ts
4
4
  var DEFAULT_FOLD_GUTTER_WIDTH = 10;
5
5
  var DEFAULT_EXPANDED_INDICATOR = "v";
6
6
  var DEFAULT_COLLAPSED_INDICATOR = ">";
@@ -251,7 +251,6 @@ function clearFoldTransition(event) {
251
251
  if (!(event.currentTarget instanceof HTMLButtonElement)) return;
252
252
  delete event.currentTarget.dataset.editorFoldTransition;
253
253
  }
254
- //#endregion
255
254
  export { createFoldGutterContribution, createFoldGutterPlugin };
256
255
 
257
256
  //# sourceMappingURL=foldGutter.js.map
@@ -5,11 +5,12 @@
5
5
  justify-content: flex-end;
6
6
  line-height: inherit;
7
7
  padding-left: 2px;
8
- padding-right: 4px;
8
+ /* The gap to the text: 8px, near Monaco's 10px and CodeMirror's 9px. */
9
+ padding-right: 8px;
9
10
  tab-size: var(--editor-tab-size, 4);
10
11
  }
11
12
 
12
- .editor-virtualized-line-number::before {
13
+ .editor-virtualized-line-number:not([hidden])::before {
13
14
  content: counter(editor-line, var(--editor-line-gutter-counter-style, decimal));
14
15
  }
15
16
 
@@ -1 +1 @@
1
- {"version":3,"file":"lineGutter.d.ts","sourceRoot":"","sources":["../src/lineGutter.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,wBAAwB,EACxB,sBAAsB,EACtB,YAAY,EACb,MAAM,mCAAmC,CAAA;AAE1C,OAAO,kBAAkB,CAAA;AAEzB,MAAM,MAAM,uBAAuB,GAAG;IACpC,QAAQ,CAAC,YAAY,CAAC,EAAE,MAAM,CAAA;IAC9B,QAAQ,CAAC,WAAW,CAAC,EAAE,CAAC,GAAG,EAAE,sBAAsB,KAAK,eAAe,CAAA;IACvE,QAAQ,CAAC,eAAe,CAAC,EAAE,MAAM,CAAA;IACjC,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,CAAA;IAC3B,QAAQ,CAAC,QAAQ,CAAC,EAAE,MAAM,CAAA;IAC1B,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,CAAA;CAC5B,CAAA;AAED,MAAM,MAAM,eAAe,GAAG,MAAM,GAAG,MAAM,GAAG,IAAI,GAAG,SAAS,CAAA;AAQhE,wBAAgB,sBAAsB,CAAC,OAAO,GAAE,uBAA4B,GAAG,YAAY,CAS1F;AAED,wBAAgB,4BAA4B,CAC1C,OAAO,GAAE,uBAA4B,GACpC,wBAAwB,CAmD1B"}
1
+ {"version":3,"file":"lineGutter.d.ts","sourceRoot":"","sources":["../src/lineGutter.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,wBAAwB,EACxB,sBAAsB,EACtB,YAAY,EACb,MAAM,mCAAmC,CAAA;AAE1C,OAAO,kBAAkB,CAAA;AAEzB,MAAM,MAAM,uBAAuB,GAAG;IACpC,QAAQ,CAAC,YAAY,CAAC,EAAE,MAAM,CAAA;IAC9B,QAAQ,CAAC,WAAW,CAAC,EAAE,CAAC,GAAG,EAAE,sBAAsB,KAAK,eAAe,CAAA;IACvE,QAAQ,CAAC,eAAe,CAAC,EAAE,MAAM,CAAA;IACjC,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,CAAA;IAC3B,QAAQ,CAAC,QAAQ,CAAC,EAAE,MAAM,CAAA;IAC1B,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,CAAA;CAC5B,CAAA;AAED,MAAM,MAAM,eAAe,GAAG,MAAM,GAAG,MAAM,GAAG,IAAI,GAAG,SAAS,CAAA;AAShE,wBAAgB,sBAAsB,CAAC,OAAO,GAAE,uBAA4B,GAAG,YAAY,CAS1F;AAED,wBAAgB,4BAA4B,CAC1C,OAAO,GAAE,uBAA4B,GACpC,wBAAwB,CAmD1B"}
@@ -1,11 +1,11 @@
1
1
  import { normalizeNonNegativeNumber, normalizePositiveInteger, setElementHidden } from "./utils.js";
2
2
  import "./lineGutter.css";
3
- //#region src/lineGutter.ts
3
+ // src/lineGutter.ts
4
4
  var DEFAULT_COUNTER_STYLE = "decimal";
5
5
  var DEFAULT_LINE_GUTTER_MIN_COLUMNS = 3;
6
6
  var DEFAULT_LINE_GUTTER_MIN_WIDTH = 26;
7
7
  var DEFAULT_START_LINE = 1;
8
- var LINE_GUTTER_PADDING_PX = 8;
8
+ var LINE_GUTTER_PADDING_PX = 12;
9
9
  function createLineGutterPlugin(options = {}) {
10
10
  const contribution = createLineGutterContribution(options);
11
11
  return {
@@ -118,7 +118,6 @@ function setTextContent(element, value) {
118
118
  function decimalDigitCount(value) {
119
119
  return String(Math.max(1, Math.floor(value))).length;
120
120
  }
121
- //#endregion
122
121
  export { createLineGutterContribution, createLineGutterPlugin };
123
122
 
124
123
  //# sourceMappingURL=lineGutter.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"lineGutter.js","names":[],"sources":["../src/lineGutter.ts"],"sourcesContent":["import type {\n EditorGutterContribution,\n EditorGutterRowContext,\n EditorPlugin,\n} from '@singapore-editor/core/extensions'\nimport { normalizeNonNegativeNumber, normalizePositiveInteger, setElementHidden } from './utils'\nimport './lineGutter.css'\n\nexport type LineGutterPluginOptions = {\n readonly counterStyle?: string\n readonly labelForRow?: (row: EditorGutterRowContext) => LineGutterLabel\n readonly minLabelColumns?: number\n readonly minDigits?: number\n readonly minWidth?: number\n readonly startLine?: number\n}\n\nexport type LineGutterLabel = number | string | null | undefined\n\nconst DEFAULT_COUNTER_STYLE = 'decimal'\nconst DEFAULT_LINE_GUTTER_MIN_COLUMNS = 3\nconst DEFAULT_LINE_GUTTER_MIN_WIDTH = 26\nconst DEFAULT_START_LINE = 1\nconst LINE_GUTTER_PADDING_PX = 8\n\nexport function createLineGutterPlugin(options: LineGutterPluginOptions = {}): EditorPlugin {\n const contribution = createLineGutterContribution(options)\n\n return {\n name: 'line-gutter',\n activate(context) {\n return context.registerGutterContribution(contribution)\n },\n }\n}\n\nexport function createLineGutterContribution(\n options: LineGutterPluginOptions = {},\n): EditorGutterContribution {\n const counterStyle = options.counterStyle ?? DEFAULT_COUNTER_STYLE\n const minLabelColumns = Math.max(\n normalizePositiveInteger(options.minLabelColumns, DEFAULT_LINE_GUTTER_MIN_COLUMNS),\n normalizePositiveInteger(options.minDigits, DEFAULT_LINE_GUTTER_MIN_COLUMNS),\n )\n const minWidth = normalizeNonNegativeNumber(options.minWidth, DEFAULT_LINE_GUTTER_MIN_WIDTH)\n const startLine = normalizePositiveInteger(options.startLine, DEFAULT_START_LINE)\n const labelForRow = options.labelForRow ?? null\n\n return {\n id: 'line-gutter',\n snapshotRenderer: labelForRow\n ? undefined\n : {\n key: JSON.stringify([\n 'line-gutter',\n 1,\n counterStyle,\n minLabelColumns,\n minWidth,\n startLine,\n ]),\n capture: captureLineGutterPaint,\n restore: restoreLineGutterPaint,\n },\n createCell(document) {\n const element = document.createElement('span')\n element.className = labelForRow\n ? 'editor-virtualized-gutter-label editor-virtualized-line-label'\n : 'editor-virtualized-gutter-label editor-virtualized-line-number'\n element.setAttribute('aria-hidden', 'true')\n if (!labelForRow) {\n element.style.setProperty('--editor-line-gutter-counter-style', counterStyle)\n }\n return element\n },\n width(context) {\n const endLine = startLine + context.lineCount - 1\n const columns = labelForRow\n ? minLabelColumns\n : Math.max(minLabelColumns, decimalDigitCount(endLine))\n return Math.max(\n minWidth,\n Math.ceil(columns * context.metrics.characterWidth + LINE_GUTTER_PADDING_PX),\n )\n },\n updateCell(element, row) {\n updateLineGutterCell(element, row, startLine, labelForRow)\n },\n }\n}\n\nfunction captureLineGutterPaint(element: HTMLElement): string | null {\n const counter = element.hidden ? '' : element.style.counterSet\n if (counter && !/^editor-line [0-9]+$/.test(counter)) return null\n return JSON.stringify({\n counter,\n hidden: element.hidden,\n active: element.classList.contains('editor-virtualized-line-number-active'),\n })\n}\n\nfunction restoreLineGutterPaint(element: HTMLElement, paint: string): boolean {\n const parsed = parseLineGutterPaint(paint)\n if (!parsed) return false\n setElementHidden(element, parsed.hidden)\n setCounterSet(element, parsed.counter)\n element.classList.toggle('editor-virtualized-line-number-active', parsed.active)\n return true\n}\n\nfunction parseLineGutterPaint(paint: string) {\n if (paint.length > 256) return null\n try {\n const value: unknown = JSON.parse(paint)\n if (value === null || typeof value !== 'object') return null\n if (!('counter' in value) || typeof value.counter !== 'string') return null\n if (!('hidden' in value) || typeof value.hidden !== 'boolean') return null\n if (!('active' in value) || typeof value.active !== 'boolean') return null\n if (!value.hidden && !/^editor-line [0-9]+$/.test(value.counter)) return null\n if (value.hidden && value.counter !== '') return null\n return { counter: value.counter, hidden: value.hidden, active: value.active }\n } catch {\n return null\n }\n}\n\nfunction updateLineGutterCell(\n element: HTMLElement,\n row: EditorGutterRowContext,\n startLine: number,\n labelForRow: ((row: EditorGutterRowContext) => LineGutterLabel) | null,\n): void {\n const label = row.primaryText && labelForRow ? labelForRow(row) : null\n const hidden = !row.primaryText || (labelForRow !== null && !hasLineGutterLabel(label))\n setElementHidden(element, hidden)\n element.classList.toggle(\n 'editor-virtualized-line-number-active',\n !hidden && row.cursorLine && row.cursorLineHighlight.gutterNumber,\n )\n if (hidden) {\n if (labelForRow) setTextContent(element, '')\n return\n }\n\n if (labelForRow) {\n setTextContent(element, String(label))\n return\n }\n\n setCounterSet(element, `editor-line ${startLine + row.bufferRow}`)\n}\n\nfunction hasLineGutterLabel(label: LineGutterLabel): boolean {\n if (label === null || label === undefined) return false\n return String(label).length > 0\n}\n\nfunction setCounterSet(element: HTMLElement, value: string): void {\n if (element.style.counterSet === value) return\n element.style.counterSet = value\n}\n\nfunction setTextContent(element: HTMLElement, value: string): void {\n if (element.textContent === value) return\n element.textContent = value\n}\n\nfunction decimalDigitCount(value: number): number {\n return String(Math.max(1, Math.floor(value))).length\n}\n"],"mappings":";;;AAmBA,IAAM,wBAAwB;AAC9B,IAAM,kCAAkC;AACxC,IAAM,gCAAgC;AACtC,IAAM,qBAAqB;AAC3B,IAAM,yBAAyB;AAE/B,SAAgB,uBAAuB,UAAmC,CAAC,GAAiB;CAC1F,MAAM,eAAe,6BAA6B,OAAO;CAEzD,OAAO;EACL,MAAM;EACN,SAAS,SAAS;GAChB,OAAO,QAAQ,2BAA2B,YAAY;EACxD;CACF;AACF;AAEA,SAAgB,6BACd,UAAmC,CAAC,GACV;CAC1B,MAAM,eAAe,QAAQ,gBAAgB;CAC7C,MAAM,kBAAkB,KAAK,IAC3B,yBAAyB,QAAQ,iBAAiB,+BAA+B,GACjF,yBAAyB,QAAQ,WAAW,+BAA+B,CAC7E;CACA,MAAM,WAAW,2BAA2B,QAAQ,UAAU,6BAA6B;CAC3F,MAAM,YAAY,yBAAyB,QAAQ,WAAW,kBAAkB;CAChF,MAAM,cAAc,QAAQ,eAAe;CAE3C,OAAO;EACL,IAAI;EACJ,kBAAkB,cACd,KAAA,IACA;GACE,KAAK,KAAK,UAAU;IAClB;IACA;IACA;IACA;IACA;IACA;GACF,CAAC;GACD,SAAS;GACT,SAAS;EACX;EACJ,WAAW,UAAU;GACnB,MAAM,UAAU,SAAS,cAAc,MAAM;GAC7C,QAAQ,YAAY,cAChB,kEACA;GACJ,QAAQ,aAAa,eAAe,MAAM;GAC1C,IAAI,CAAC,aACH,QAAQ,MAAM,YAAY,sCAAsC,YAAY;GAE9E,OAAO;EACT;EACA,MAAM,SAAS;G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,MAAM;GAAS,QAAQ,MAAM;GAAQ,QAAQ,MAAM;EAAO;CAC9E,QAAQ;EACN,OAAO;CACT;AACF;AAEA,SAAS,qBACP,SACA,KACA,WACA,aACM;CACN,MAAM,QAAQ,IAAI,eAAe,cAAc,YAAY,GAAG,IAAI;CAClE,MAAM,SAAS,CAAC,IAAI,eAAgB,gBAAgB,QAAQ,CAAC,mBAAmB,KAAK;CACrF,iBAAiB,SAAS,MAAM;CAChC,QAAQ,UAAU,OAChB,yCACA,CAAC,UAAU,IAAI,cAAc,IAAI,oBAAoB,YACvD;CACA,IAAI,QAAQ;EACV,IAAI,aAAa,eAAe,SAAS,EAAE;EAC3C;CACF;CAEA,IAAI,aAAa;EACf,eAAe,SAAS,OAAO,KAAK,CAAC;EACrC;CACF;CAEA,cAAc,SAAS,eAAe,YAAY,IAAI,WAAW;AACnE;AAEA,SAAS,mBAAmB,OAAiC;CAC3D,IAAI,UAAU,QAAQ,UAAU,KAAA,GAAW,OAAO;CAClD,OAAO,OAAO,KAAK,CAAC,CAAC,SAAS;AAChC;AAEA,SAAS,cAAc,SAAsB,OAAqB;CAChE,IAAI,QAAQ,MAAM,eAAe,OAAO;CACxC,QAAQ,MAAM,aAAa;AAC7B;AAEA,SAAS,eAAe,SAAsB,OAAqB;CACjE,IAAI,QAAQ,gBAAgB,OAAO;CACnC,QAAQ,cAAc;AACxB;AAEA,SAAS,kBAAkB,OAAuB;CAChD,OAAO,OAAO,KAAK,IAAI,GAAG,KAAK,MAAM,KAAK,CAAC,CAAC,CAAC,CAAC;AAChD"}
1
+ {"version":3,"file":"lineGutter.js","names":[],"sources":["../src/lineGutter.ts"],"sourcesContent":["import type {\n EditorGutterContribution,\n EditorGutterRowContext,\n EditorPlugin,\n} from '@singapore-editor/core/extensions'\nimport { normalizeNonNegativeNumber, normalizePositiveInteger, setElementHidden } from './utils'\nimport './lineGutter.css'\n\nexport type LineGutterPluginOptions = {\n readonly counterStyle?: string\n readonly labelForRow?: (row: EditorGutterRowContext) => LineGutterLabel\n readonly minLabelColumns?: number\n readonly minDigits?: number\n readonly minWidth?: number\n readonly startLine?: number\n}\n\nexport type LineGutterLabel = number | string | null | undefined\n\nconst DEFAULT_COUNTER_STYLE = 'decimal'\nconst DEFAULT_LINE_GUTTER_MIN_COLUMNS = 3\nconst DEFAULT_LINE_GUTTER_MIN_WIDTH = 26\nconst DEFAULT_START_LINE = 1\n// The label's 2px left and 8px right padding, plus 2px so the widest number never touches either.\nconst LINE_GUTTER_PADDING_PX = 12\n\nexport function createLineGutterPlugin(options: LineGutterPluginOptions = {}): EditorPlugin {\n const contribution = createLineGutterContribution(options)\n\n return {\n name: 'line-gutter',\n activate(context) {\n return context.registerGutterContribution(contribution)\n },\n }\n}\n\nexport function createLineGutterContribution(\n options: LineGutterPluginOptions = {},\n): EditorGutterContribution {\n const counterStyle = options.counterStyle ?? DEFAULT_COUNTER_STYLE\n const minLabelColumns = Math.max(\n normalizePositiveInteger(options.minLabelColumns, DEFAULT_LINE_GUTTER_MIN_COLUMNS),\n normalizePositiveInteger(options.minDigits, DEFAULT_LINE_GUTTER_MIN_COLUMNS),\n )\n const minWidth = normalizeNonNegativeNumber(options.minWidth, DEFAULT_LINE_GUTTER_MIN_WIDTH)\n const startLine = normalizePositiveInteger(options.startLine, DEFAULT_START_LINE)\n const labelForRow = options.labelForRow ?? null\n\n return {\n id: 'line-gutter',\n snapshotRenderer: labelForRow\n ? undefined\n : {\n key: JSON.stringify([\n 'line-gutter',\n 1,\n counterStyle,\n minLabelColumns,\n minWidth,\n startLine,\n ]),\n capture: captureLineGutterPaint,\n restore: restoreLineGutterPaint,\n },\n createCell(document) {\n const element = document.createElement('span')\n element.className = labelForRow\n ? 'editor-virtualized-gutter-label editor-virtualized-line-label'\n : 'editor-virtualized-gutter-label editor-virtualized-line-number'\n element.setAttribute('aria-hidden', 'true')\n if (!labelForRow) {\n element.style.setProperty('--editor-line-gutter-counter-style', counterStyle)\n }\n return element\n },\n width(context) {\n const endLine = startLine + context.lineCount - 1\n const columns = labelForRow\n ? minLabelColumns\n : Math.max(minLabelColumns, decimalDigitCount(endLine))\n return Math.max(\n minWidth,\n Math.ceil(columns * context.metrics.characterWidth + LINE_GUTTER_PADDING_PX),\n )\n },\n updateCell(element, row) {\n updateLineGutterCell(element, row, startLine, labelForRow)\n },\n }\n}\n\nfunction captureLineGutterPaint(element: HTMLElement): string | null {\n const counter = element.hidden ? '' : element.style.counterSet\n if (counter && !/^editor-line [0-9]+$/.test(counter)) return null\n return JSON.stringify({\n counter,\n hidden: element.hidden,\n active: element.classList.contains('editor-virtualized-line-number-active'),\n })\n}\n\nfunction restoreLineGutterPaint(element: HTMLElement, paint: string): boolean {\n const parsed = parseLineGutterPaint(paint)\n if (!parsed) return false\n setElementHidden(element, parsed.hidden)\n setCounterSet(element, parsed.counter)\n element.classList.toggle('editor-virtualized-line-number-active', parsed.active)\n return true\n}\n\nfunction parseLineGutterPaint(paint: string) {\n if (paint.length > 256) return null\n try {\n const value: unknown = JSON.parse(paint)\n if (value === null || typeof value !== 'object') return null\n if (!('counter' in value) || typeof value.counter !== 'string') return null\n if (!('hidden' in value) || typeof value.hidden !== 'boolean') return null\n if (!('active' in value) || typeof value.active !== 'boolean') return null\n if (!value.hidden && !/^editor-line [0-9]+$/.test(value.counter)) return null\n if (value.hidden && value.counter !== '') return null\n return { counter: value.counter, hidden: value.hidden, active: value.active }\n } catch {\n return null\n }\n}\n\nfunction updateLineGutterCell(\n element: HTMLElement,\n row: EditorGutterRowContext,\n startLine: number,\n labelForRow: ((row: EditorGutterRowContext) => LineGutterLabel) | null,\n): void {\n const label = row.primaryText && labelForRow ? labelForRow(row) : null\n const hidden = !row.primaryText || (labelForRow !== null && !hasLineGutterLabel(label))\n setElementHidden(element, hidden)\n element.classList.toggle(\n 'editor-virtualized-line-number-active',\n !hidden && row.cursorLine && row.cursorLineHighlight.gutterNumber,\n )\n if (hidden) {\n if (labelForRow) setTextContent(element, '')\n return\n }\n\n if (labelForRow) {\n setTextContent(element, String(label))\n return\n }\n\n setCounterSet(element, `editor-line ${startLine + row.bufferRow}`)\n}\n\nfunction hasLineGutterLabel(label: LineGutterLabel): boolean {\n if (label === null || label === undefined) return false\n return String(label).length > 0\n}\n\nfunction setCounterSet(element: HTMLElement, value: string): void {\n if (element.style.counterSet === value) return\n element.style.counterSet = value\n}\n\nfunction setTextContent(element: HTMLElement, value: string): void {\n if (element.textContent === value) return\n element.textContent = value\n}\n\nfunction decimalDigitCount(value: number): number {\n return String(Math.max(1, Math.floor(value))).length\n}\n"],"mappings":";;;AAmBA,IAAM,wBAAwB;AAC9B,IAAM,kCAAkC;AACxC,IAAM,gCAAgC;AACtC,IAAM,qBAAqB;AAE3B,IAAM,yBAAyB;AAE/B,SAAgB,uBAAuB,UAAmC,CAAC,GAAiB;CAC1F,MAAM,eAAe,6BAA6B,OAAO;CAEzD,OAAO;EACL,MAAM;EACN,SAAS,SAAS;GAChB,OAAO,QAAQ,2BAA2B,YAAY;EACxD;CACF;AACF;AAEA,SAAgB,6BACd,UAAmC,CAAC,GACV;CAC1B,MAAM,eAAe,QAAQ,gBAAgB;CAC7C,MAAM,kBAAkB,KAAK,IAC3B,yBAAyB,QAAQ,iBAAiB,+BAA+B,GACjF,yBAAyB,QAAQ,WAAW,+BAA+B,CAC7E;CACA,MAAM,WAAW,2BAA2B,QAAQ,UAAU,6BAA6B;CAC3F,MAAM,YAAY,yBAAyB,QAAQ,WAAW,kBAAkB;CAChF,MAAM,cAAc,QAAQ,eAAe;CAE3C,OAAO;EACL,IAAI;EACJ,kBAAkB,cACd,KAAA,IACA;GACE,KAAK,KAAK,UAAU;IAClB;IACA;IACA;IACA;IACA;IACA;GACF,CAAC;GACD,SAAS;GACT,SAAS;EACX;EACJ,WAAW,UAAU;GACnB,MAAM,UAAU,SAAS,cAAc,MAAM;GAC7C,QAAQ,YAAY,cAChB,kEACA;GACJ,QAAQ,aAAa,eAAe,MAAM;GAC1C,IAAI,CAAC,aACH,QAAQ,MAAM,YA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,UAAU,MAAM,YAAY,IAAI,OAAO;EACjD,OAAO;GAAE,SAAS,MAAM;GAAS,QAAQ,MAAM;GAAQ,QAAQ,MAAM;EAAO;CAC9E,QAAQ;EACN,OAAO;CACT;AACF;AAEA,SAAS,qBACP,SACA,KACA,WACA,aACM;CACN,MAAM,QAAQ,IAAI,eAAe,cAAc,YAAY,GAAG,IAAI;CAClE,MAAM,SAAS,CAAC,IAAI,eAAgB,gBAAgB,QAAQ,CAAC,mBAAmB,KAAK;CACrF,iBAAiB,SAAS,MAAM;CAChC,QAAQ,UAAU,OAChB,yCACA,CAAC,UAAU,IAAI,cAAc,IAAI,oBAAoB,YACvD;CACA,IAAI,QAAQ;EACV,IAAI,aAAa,eAAe,SAAS,EAAE;EAC3C;CACF;CAEA,IAAI,aAAa;EACf,eAAe,SAAS,OAAO,KAAK,CAAC;EACrC;CACF;CAEA,cAAc,SAAS,eAAe,YAAY,IAAI,WAAW;AACnE;AAEA,SAAS,mBAAmB,OAAiC;CAC3D,IAAI,UAAU,QAAQ,UAAU,KAAA,GAAW,OAAO;CAClD,OAAO,OAAO,KAAK,CAAC,CAAC,SAAS;AAChC;AAEA,SAAS,cAAc,SAAsB,OAAqB;CAChE,IAAI,QAAQ,MAAM,eAAe,OAAO;CACxC,QAAQ,MAAM,aAAa;AAC7B;AAEA,SAAS,eAAe,SAAsB,OAAqB;CACjE,IAAI,QAAQ,gBAAgB,OAAO;CACnC,QAAQ,cAAc;AACxB;AAEA,SAAS,kBAAkB,OAAuB;CAChD,OAAO,OAAO,KAAK,IAAI,GAAG,KAAK,MAAM,KAAK,CAAC,CAAC,CAAC,CAAC;AAChD"}
package/dist/utils.js CHANGED
@@ -1,4 +1,4 @@
1
- //#region src/utils.ts
1
+ // src/utils.ts
2
2
  function setElementHidden(element, hidden) {
3
3
  if (element.hidden === hidden) return;
4
4
  element.hidden = hidden;
@@ -16,7 +16,6 @@ function normalizeNonNegativeNumber(value, fallback) {
16
16
  if (!Number.isFinite(value) || value === void 0 || value < 0) return fallback;
17
17
  return value;
18
18
  }
19
- //#endregion
20
19
  export { addClassName, normalizeNonNegativeNumber, normalizePositiveInteger, setElementHidden };
21
20
 
22
21
  //# sourceMappingURL=utils.js.map
package/package.json CHANGED
@@ -1,6 +1,24 @@
1
1
  {
2
2
  "name": "@singapore-editor/gutters",
3
- "version": "0.1.1",
3
+ "version": "0.2.11",
4
+ "description": "Line numbers and fold controls for the Singapore editor.",
5
+ "keywords": [
6
+ "browser",
7
+ "editor",
8
+ "folding",
9
+ "line-numbers",
10
+ "singapore",
11
+ "typescript"
12
+ ],
13
+ "homepage": "https://singapore.shaulavo.dev/docs/reference/api/gutters/overview/",
14
+ "bugs": {
15
+ "url": "https://github.com/ShaulLavo/singapore/issues"
16
+ },
17
+ "license": "MIT",
18
+ "repository": {
19
+ "type": "git",
20
+ "url": "git+https://github.com/ShaulLavo/singapore.git"
21
+ },
4
22
  "files": [
5
23
  "README.md",
6
24
  "dist"
@@ -36,25 +54,25 @@
36
54
  },
37
55
  "publishConfig": {
38
56
  "access": "public",
57
+ "provenance": true,
39
58
  "registry": "https://registry.npmjs.org/"
40
59
  },
41
60
  "scripts": {
42
61
  "build": "bun ../../scripts/build-package.ts",
43
62
  "test": "vitest run",
44
- "typecheck": "tsgo --noEmit",
63
+ "typecheck": "tsc --noEmit",
45
64
  "lint": "oxlint .",
46
65
  "format": "oxfmt --write .",
47
66
  "format:check": "oxfmt --check ."
48
67
  },
49
68
  "dependencies": {
50
- "@singapore-editor/core": "0.1.2"
69
+ "@singapore-editor/core": "0.2.11"
51
70
  },
52
71
  "devDependencies": {
53
- "@typescript/native-preview": "7.0.0-dev.20260707.2",
54
72
  "happy-dom": "^20.14.5",
55
73
  "oxfmt": "0.70.0",
56
74
  "oxlint": "1.85.0",
57
- "typescript": "~6.0.3",
58
- "vitest": "^5.0.2"
75
+ "typescript": "7.0.2",
76
+ "vitest": "5.0.2"
59
77
  }
60
78
  }