MCPcopy Create free account
hub / github.com/Siris2314/NecroAtomicBot / wrapText

Function wrapText

classes/functions.js:144–173  ·  view source on GitHub ↗
(ctx, text, maxWidth)

Source from the content-addressed store, hash-verified

142 },
143
144 wrapText (ctx, text, maxWidth) {
145 return new Promise(resolve => {
146 if (ctx.measureText(text).width < maxWidth) return resolve([text]);
147 if (ctx.measureText('W').width > maxWidth) return resolve(null);
148 const words = text.split(' ');
149 const lines = [];
150 let line = '';
151 while (words.length > 0) {
152 let split = false;
153 while (ctx.measureText(words[0]).width >= maxWidth) {
154 const temp = words[0];
155 words[0] = temp.slice(0, -1);
156 if (split) {
157 words[1] = `${temp.slice(-1)}${words[1]}`;
158 } else {
159 split = true;
160 words.splice(1, 0, temp.slice(-1));
161 }
162 }
163 if (ctx.measureText(`${line}${words[0]}`).width < maxWidth) {
164 line += `${words.shift()} `;
165 } else {
166 lines.push(line.trim());
167 line = '';
168 }
169 if (words.length === 0) lines.push(line.trim());
170 }
171 return resolve(lines);
172 });
173 },
174
175}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected