MCPcopy Create free account
hub / github.com/adobe/react-spectrum / CodeFold

Function CodeFold

packages/dev/s2-docs/src/CodeFold.tsx:58–154  ·  view source on GitHub ↗
({tokens})

Source from the content-addressed store, hash-verified

56});
57
58export function CodeFold({tokens}) {
59 let ref = useRef(null);
60 let [firstLine, collapsed, lastLine] = useMemo(() => {
61 let firstLine: Token[] = [];
62 let collapsed: Token[] = [];
63 let lastLine: Token[] = [];
64
65 // Iterate forward to find the first newline.
66 for (let [i, child] of tokens.entries()) {
67 if (typeof child === 'string') {
68 let index = child.indexOf('\n');
69 if (index === 0) {
70 collapsed = tokens.slice(i);
71 break;
72 } else if (index > 0) {
73 firstLine.push(child.slice(0, index));
74 if (index < child.length) {
75 collapsed.push(child.slice(index));
76 }
77 collapsed.push(...tokens.slice(i + 1));
78 break;
79 }
80 }
81
82 firstLine.push(child);
83 }
84
85 // Iterate backward to find the last newline.
86 for (let i = collapsed.length - 1; i >= 0; i--) {
87 let child = collapsed[i];
88 if (typeof child === 'string') {
89 let c = child;
90 if (i === collapsed.length - 1) {
91 // Ignore the last newline if it is at the very end of the code.
92 c = c.trimEnd();
93 }
94 let index = c.lastIndexOf('\n');
95 if (index === 0) {
96 lastLine.unshift(c);
97 break;
98 } else if (index > 0) {
99 lastLine.unshift(c.slice(index));
100 break;
101 }
102 }
103
104 lastLine.unshift(child);
105 }
106
107 if (typeof collapsed[0] === 'string') {
108 collapsed[0] = collapsed[0].replace(/^\n/, '');
109 }
110
111 if (typeof lastLine[0] === 'string') {
112 lastLine[0] = lastLine[0].trimStart();
113 }
114
115 return [firstLine, collapsed, lastLine];

Callers

nothing calls this directly

Calls 3

pressScaleFunction · 0.90
sliceMethod · 0.80
pushMethod · 0.80

Tested by

no test coverage detected