MCPcopy Create free account
hub / github.com/microsoft/SandDance / breakWord

Function breakWord

docs/app/js/sanddance-app.js:106045–106084  ·  view source on GitHub ↗
(text, maxWidth, iconMapping)

Source from the content-addressed store, hash-verified

106043 };
106044}
106045function breakWord(text, maxWidth, iconMapping) {
106046 var rows = [];
106047 var rowStartCharIndex = 0;
106048 var groupStartCharIndex = 0;
106049 var rowOffsetLeft = 0;
106050 var group = null;
106051 for(var i = 0; i < text.length; i++){
106052 if (text[i] === " ") {
106053 group = text[i];
106054 groupStartCharIndex = i + 1;
106055 } else if (i + 1 < text.length && text[i + 1] === " " || i + 1 === text.length) {
106056 group = text.substring(groupStartCharIndex, i + 1);
106057 groupStartCharIndex = i + 1;
106058 } else group = null;
106059 if (group) {
106060 var groupWidth = getTextWidth(group, iconMapping);
106061 if (rowOffsetLeft + groupWidth > maxWidth) {
106062 var lastGroupStartIndex = groupStartCharIndex - group.length;
106063 if (rowStartCharIndex < lastGroupStartIndex) {
106064 rows.push(text.substring(rowStartCharIndex, lastGroupStartIndex));
106065 rowStartCharIndex = lastGroupStartIndex;
106066 rowOffsetLeft = 0;
106067 }
106068 if (groupWidth > maxWidth) {
106069 var subGroups = breakAll(group, maxWidth, iconMapping);
106070 if (subGroups.rows.length > 1) rows = rows.concat(subGroups.rows.slice(0, subGroups.rows.length - 1));
106071 rowStartCharIndex = rowStartCharIndex + subGroups.lastRowStartCharIndex;
106072 groupWidth = subGroups.lastRowOffsetLeft;
106073 }
106074 }
106075 rowOffsetLeft += groupWidth;
106076 }
106077 }
106078 if (rowStartCharIndex < text.length) rows.push(text.substring(rowStartCharIndex));
106079 return {
106080 rows: rows,
106081 lastRowStartCharIndex: rowStartCharIndex,
106082 lastRowOffsetLeft: rowOffsetLeft
106083 };
106084}
106085function autoWrapping(text, wordBreak, maxWidth, iconMapping) {
106086 if (wordBreak === "break-all") return breakAll(text, maxWidth, iconMapping);
106087 return breakWord(text, maxWidth, iconMapping);

Callers 1

autoWrappingFunction · 0.70

Calls 2

getTextWidthFunction · 0.70
breakAllFunction · 0.70

Tested by

no test coverage detected