Skip to content

Commit 274c8d8

Browse files
authored
experimental: adapt tailwind to webstudio breakpoints (#5244)
Ref #2651 There is an edge case which will prevent from converting breakpoints. When there is no base breakpoint there is no value to fill with inverted space. Though it should be very rare usecase. Need to test how often it will create new breakpoints.
1 parent aea46ab commit 274c8d8

File tree

5 files changed

+497
-83
lines changed

5 files changed

+497
-83
lines changed

apps/builder/app/shared/copy-paste/plugin-markdown.test.tsx

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,11 @@ test("inline code", () => {
141141
});
142142

143143
test("code", () => {
144-
expect(parse("```js meta\nfoo\n```")).toEqual(
144+
expect(parse("```js meta\nfoo\nbar\n```")).toEqual(
145145
renderTemplate(
146146
<ws.element ws:tag="pre">
147147
<ws.element ws:tag="code" class="language-js">
148-
{"foo "}
148+
{"foo\nbar\n"}
149149
</ws.element>
150150
</ws.element>
151151
)
@@ -177,13 +177,30 @@ test("thematic break | separator", () => {
177177
});
178178

179179
test("strikethrough", () => {
180-
expect(parse("~One~ ~~two~~ ~~~three~~~.")).toEqual(
180+
expect(parse("~One~\n\n~~two~~")).toEqual(
181181
renderTemplate(
182-
<ws.element ws:tag="p">
183-
<ws.element ws:tag="del">One</ws.element>
184-
<ws.element ws:tag="del">two</ws.element>
185-
<ws.element ws:tag="span"> ~~~three~~~.</ws.element>
186-
</ws.element>
182+
<>
183+
<ws.element ws:tag="p">
184+
<ws.element ws:tag="del">One</ws.element>
185+
</ws.element>
186+
<ws.element ws:tag="p">
187+
<ws.element ws:tag="del">two</ws.element>
188+
</ws.element>
189+
</>
190+
)
191+
);
192+
});
193+
194+
test("preserve spaces between strong and em", () => {
195+
expect(parse("**One** *two* text")).toEqual(
196+
renderTemplate(
197+
<>
198+
<ws.element ws:tag="p">
199+
<ws.element ws:tag="strong">One</ws.element>{" "}
200+
<ws.element ws:tag="em">two</ws.element>
201+
{" text"}
202+
</ws.element>
203+
</>
187204
)
188205
);
189206
});

apps/builder/app/shared/html.test.tsx

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,9 +199,26 @@ test("collapse any spacing characters inside text", () => {
199199
another line
200200
</div>
201201
`)
202+
).toEqual(
203+
renderTemplate(<ws.element ws:tag="div">{"line another line"}</ws.element>)
204+
);
205+
});
206+
207+
test("collapse any spacing characters inside rich text", () => {
208+
expect(
209+
generateFragmentFromHtml(`
210+
<div>
211+
<i> line </i>
212+
<b> another line </b>
213+
text
214+
</div>
215+
`)
202216
).toEqual(
203217
renderTemplate(
204-
<ws.element ws:tag="div">{" line another line "}</ws.element>
218+
<ws.element ws:tag="div">
219+
<ws.element ws:tag="i">line</ws.element>{" "}
220+
<ws.element ws:tag="b">another line</ws.element> text
221+
</ws.element>
205222
)
206223
);
207224
});

apps/builder/app/shared/html.ts

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -231,22 +231,37 @@ export const generateFragmentFromHtml = (
231231
}
232232
}
233233
}
234-
for (const childNode of node.childNodes) {
234+
for (let index = 0; index < node.childNodes.length; index += 1) {
235+
const childNode = node.childNodes[index];
235236
if (defaultTreeAdapter.isElementNode(childNode)) {
236237
const child = convertElementToInstance(childNode);
237238
if (child) {
238239
instance.children.push(child);
239240
}
240241
}
241242
if (defaultTreeAdapter.isTextNode(childNode)) {
242-
if (spaceRegex.test(childNode.value)) {
243-
continue;
243+
// trim spaces around rich text
244+
// do not for code
245+
if (spaceRegex.test(childNode.value) && node.tagName !== "code") {
246+
if (index === 0 || index === node.childNodes.length - 1) {
247+
continue;
248+
}
244249
}
245250
let child: Instance["children"][number] = {
246251
type: "text",
247-
// collapse spacing characters inside of text to avoid preserved newlines
248-
value: childNode.value.replaceAll(/\s+/g, " "),
252+
value: childNode.value,
249253
};
254+
if (node.tagName !== "code") {
255+
// collapse spacing characters inside of text to avoid preserved newlines
256+
child.value = child.value.replaceAll(/\s+/g, " ");
257+
// remove unnecessary spacing in nodes
258+
if (index === 0) {
259+
child.value = child.value.trimStart();
260+
}
261+
if (index === node.childNodes.length - 1) {
262+
child.value = child.value.trimEnd();
263+
}
264+
}
250265
// textarea content is initial value
251266
// and represented with fake value attribute
252267
if (node.tagName === "textarea") {
@@ -271,6 +286,10 @@ export const generateFragmentFromHtml = (
271286
// <article></article>
272287
// </div>
273288
if (hasNonRichTextContent) {
289+
// remove spaces between elements outside of rich text
290+
if (spaceRegex.test(childNode.value)) {
291+
continue;
292+
}
274293
const span: Instance = {
275294
type: "instance",
276295
id: getNewId(),

0 commit comments

Comments
 (0)