Debian Codegraph @

debian/missing-sources/codejar-linenumbers.js

raw
1 // Source: https://github.com/julianpoemp/codejar-linenumbers/blob/main/src/codejar-linenumbers.ts
2 // This class was copied from https://github.com/antonmedv/codejar/blob/3.7.0/linenumbers.ts
3 // and applied with further improvements and fixes
4
5 type Options = {
6 class: string
7 wrapClass: string
8 width: string
9 backgroundColor: string
10 color: string
11 }
12
13 export function withLineNumbers(
14 highlight: (e: HTMLElement) => void,
15 options: Partial<Options> = {}
16 ) {
17 const opts: Options = {
18 class: "codejar-linenumbers",
19 wrapClass: "codejar-wrap",
20 width: "35px",
21 backgroundColor: "rgba(128, 128, 128, 0.15)",
22 color: "",
23 ...options
24 }
25
26 let lineNumbers: HTMLElement
27 return function (editor: HTMLElement) {
28 highlight(editor)
29
30 if (!lineNumbers) {
31 lineNumbers = init(editor, opts)
32 editor.addEventListener("scroll", () => lineNumbers.style.top = `-${editor.scrollTop}px`);
33 }
34
35 const code = editor.textContent || ""
36 const linesCount = code.replace(/\n$/g, "").split("\n").length
37
38 let text = ""
39 for (let i = 0; i < linesCount; i++) {
40 text += `${i + 1}\n`
41 }
42
43 lineNumbers.innerText = text
44 }
45 }
46
47 function init(editor: HTMLElement, opts: Options): HTMLElement {
48 const css = getComputedStyle(editor);
49
50 const wrap = document.createElement("div")
51 wrap.className = opts.wrapClass
52 wrap.style.position = "relative"
53
54 const innerWrap = document.createElement("div");
55 innerWrap.className = "codejar-linenumbers-inner-wrap";
56 innerWrap.style.background = css.background;
57 innerWrap.style.marginTop = css.borderTopWidth;
58 innerWrap.style.marginBottom = css.borderBottomWidth;
59 innerWrap.style.marginLeft = css.borderLeftWidth;
60 innerWrap.style.borderTopLeftRadius = css.borderTopLeftRadius;
61 innerWrap.style.borderBottomLeftRadius = css.borderBottomLeftRadius;
62
63 const gutter = document.createElement("div")
64 gutter.className = opts.class;
65 innerWrap.appendChild(gutter);
66
67 wrap.appendChild(innerWrap)
68
69 // Add own styles
70 gutter.style.width = opts.width
71 gutter.style.overflow = "hidden"
72 gutter.style.backgroundColor = opts.backgroundColor
73
74 // Copy editor styles
75 gutter.style.fontFamily = css.fontFamily
76 gutter.style.fontSize = css.fontSize
77 gutter.style.lineHeight = css.lineHeight
78 gutter.style.paddingTop = `calc(${css.paddingTop})`;
79 gutter.style.paddingLeft = css.paddingLeft
80 gutter.style.borderTopLeftRadius = css.borderTopLeftRadius
81 gutter.style.borderBottomLeftRadius = css.borderBottomLeftRadius;
82
83 // Add line numbers
84 const lineNumbers = document.createElement("div");
85 lineNumbers.setAttribute("class", "codejar-linenumber");
86 lineNumbers.style.color = opts.color || css.color;
87 lineNumbers.style.setProperty("mix-blend-mode", "unset");
88 gutter.appendChild(lineNumbers)
89
90 // Tweak editor styles
91 editor.style.paddingLeft = `calc(${opts.width} + ${gutter.style.paddingLeft} + 5px)`
92 editor.style.whiteSpace = "pre"
93
94 // Swap editor with a wrap
95 editor.parentNode!.insertBefore(wrap, editor)
96 wrap.appendChild(editor)
97 return lineNumbers
98 }