From 99c74385ea1bc3a16e05c7b49c0c57d1999bbe23 Mon Sep 17 00:00:00 2001 From: Harsh Singh Date: Mon, 11 Sep 2023 12:49:38 +0530 Subject: [PATCH] fix windows cache dir --- Cargo.lock | 2 +- fastn-core/src/package/mod.rs | 9 +- fastn-core/src/package/redirects.rs | 51 +- fastn-core/src/utils.rs | 13 +- fastn-js/js/dom.js | 60 +- fastn-js/js/fastn.js | 2 +- fastn-js/src/property.rs | 2 + fastn-js/src/to_js.rs | 3 + fastn/Cargo.toml | 2 +- ftd/src/interpreter/things/default.rs | 6 + ftd/src/js/element.rs | 12 + ftd/t/js/01-basic-module.html | 22 +- ftd/t/js/02-property.html | 10 +- ftd/t/js/03-common-properties.html | 735 ++++++++++++++----- ftd/t/js/04-variable.html | 7 +- ftd/t/js/05-dynamic-dom-list.html | 9 +- ftd/t/js/06-dynamic-dom-list-2.html | 6 +- ftd/t/js/08-inherited.html | 37 +- ftd/t/js/09-text-properties.html | 88 ++- ftd/t/js/10-color-test.html | 26 +- ftd/t/js/12-children.html | 5 +- ftd/t/js/13-non-style-properties.html | 24 +- ftd/t/js/14-code.html | 9 +- ftd/t/js/15-function-call-in-property.html | 3 + ftd/t/js/17-clone.html | 13 +- ftd/t/js/18-rive.html | 9 +- ftd/t/js/20-background-properties.html | 43 +- ftd/t/js/23-record-list.html | 8 +- ftd/t/js/24-device.html | 3 +- ftd/t/js/29-dom-list.html | 10 +- ftd/t/js/30-web-component.html | 89 ++- ftd/t/js/31-advance-list.html | 34 +- ftd/t/js/31-ftd-len.html | 5 +- ftd/t/js/32-ftd-len.html | 32 +- ftd/t/js/33-list-indexing.html | 6 +- ftd/t/js/36-single-ui.html | 3 +- ftd/t/js/37-expander.html | 42 +- ftd/t/js/38-background-image-properties.html | 11 +- ftd/t/js/40-code-themes.html | 13 +- ftd/t/js/43-image-object-fit.html | 31 +- ftd/t/js/44-local-storage.html | 3 +- ftd/t/js/44-module.html | 7 +- ftd/t/js/45-re-module.html | 7 +- ftd/t/js/47-ftd-code-syntax.html | 9 +- ftd/t/js/49-align-content.html | 251 ++++++- ftd/t/js/50-iframe-fullscreen.html | 15 +- ftd/t/js/51-markdown-table.html | 13 +- ftd/t/js/52-events.ftd | 96 +++ ftd/t/js/52-events.html | 229 ++++++ ftd/t/js/53-link-color.ftd | 14 + ftd/t/js/53-link-color.html | 90 +++ ftd/t/js/54-class-fix.ftd | 10 + ftd/t/js/54-class-fix.html | 93 +++ 53 files changed, 1890 insertions(+), 442 deletions(-) create mode 100644 ftd/t/js/52-events.ftd create mode 100644 ftd/t/js/52-events.html create mode 100644 ftd/t/js/53-link-color.ftd create mode 100644 ftd/t/js/53-link-color.html create mode 100644 ftd/t/js/54-class-fix.ftd create mode 100644 ftd/t/js/54-class-fix.html diff --git a/Cargo.lock b/Cargo.lock index 08c28736aa..8c933a53b1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1519,7 +1519,7 @@ dependencies = [ [[package]] name = "fastn" -version = "0.3.42" +version = "0.3.49" dependencies = [ "clap", "colored", diff --git a/fastn-core/src/package/mod.rs b/fastn-core/src/package/mod.rs index f8e2e75781..b1da0f15bf 100644 --- a/fastn-core/src/package/mod.rs +++ b/fastn-core/src/package/mod.rs @@ -597,7 +597,14 @@ impl Package { package.redirects = { let redirects_temp: Option = fastn_doc.get("fastn#redirects")?; - redirects_temp.map(|r| r.redirects_from_body()) + if let Some(redirects) = redirects_temp { + let result = redirects + .redirects_from_body() + .map_err(|e| fastn_core::Error::GenericError(e.to_string()))?; + Some(result) + } else { + None + } }; package.auto_import = fastn_doc diff --git a/fastn-core/src/package/redirects.rs b/fastn-core/src/package/redirects.rs index 040f5284d3..4978bd4b80 100644 --- a/fastn-core/src/package/redirects.rs +++ b/fastn-core/src/package/redirects.rs @@ -5,19 +5,62 @@ pub struct RedirectsTemp { } impl RedirectsTemp { - pub(crate) fn redirects_from_body(&self) -> ftd::Map { + pub(crate) fn redirects_from_body(&self) -> fastn_core::Result> { let body = self.body.as_str(); let mut redirects: ftd::Map = ftd::Map::new(); for line in body.lines() { - if line.trim_start().starts_with(';') { + if line.is_empty() || line.trim_start().starts_with(';') { + continue; + } + // Supported Redirects Syntax under fastn.redirects + // : + // -> + + if let Some((key, value)) = line.split_once("->") { + Self::assert_and_insert_redirect(key, value, &mut redirects)?; continue; } if let Some((key, value)) = line.split_once(':') { - redirects.insert(key.trim().to_owned(), value.trim().to_owned()); + fastn_core::warning!( + "Redirect syntax: '{key}: {value}' will be deprecated\nPlease use the '{key} \ + -> {value}' redirect syntax instead." + ); + Self::assert_and_insert_redirect(key, value, &mut redirects)?; } } - redirects + Ok(redirects) + } + + // Assert checks on redirects + // - All redirects should be A -> B where A != B (Self loop) + // - If A -> B exists then there can’t be A -> C where B != C + // (No duplicated values starting with the same A) + fn assert_and_insert_redirect( + from: &str, + to: &str, + redirects: &mut ftd::Map, + ) -> fastn_core::Result<()> { + let from = from.trim().to_owned(); + let to = to.trim().to_owned(); + + assert!( + !from.eq(to.as_str()), + "Redirect {} -> {} is invalid", + from, + to + ); + assert!( + !redirects.contains_key(from.as_str()), + "Redirect {} -> {} is invalid, since {} -> {} already exists", + from.as_str(), + to.as_str(), + from.as_str(), + redirects.get(from.as_str()).unwrap(), + ); + + redirects.insert(from, to); + Ok(()) } } diff --git a/fastn-core/src/utils.rs b/fastn-core/src/utils.rs index d4f81dc11d..efd7d3b76e 100644 --- a/fastn-core/src/utils.rs +++ b/fastn-core/src/utils.rs @@ -42,9 +42,18 @@ pub fn get_ftd_hash(path: &str) -> fastn_core::Result { } pub fn get_cache_file(id: &str) -> Option { + let cache_dir = dirs::cache_dir()?; + let base_path = cache_dir.join("fastn.com"); + + if !base_path.exists() { + if let Err(err) = std::fs::create_dir_all(&base_path) { + eprintln!("Failed to create cache directory: {}", err); + return None; + } + } + Some( - dirs::cache_dir()? - .join("fastn.com/") + base_path .join(id_to_cache_key( &std::env::current_dir() .expect("cant read current dir") diff --git a/fastn-js/js/dom.js b/fastn-js/js/dom.js index 5ef5652611..df3d4474e2 100644 --- a/fastn-js/js/dom.js +++ b/fastn-js/js/dom.js @@ -60,6 +60,7 @@ fastn_dom.propertyMap = { "justify-content": "jc", "left": "l", "link": "lk", + "link-color": "lkc", "margin": "m", "margin-bottom": "mb", "margin-horizontal": "mh", @@ -273,6 +274,7 @@ fastn_dom.PropertyKind = { LoopVideo: 113, Controls: 114, Muted: 115, + LinkColor: 116, }; @@ -870,7 +872,7 @@ class Node2 { attachCss(property, value, createClass, className) { let propertyShort = fastn_dom.propertyMap[property] || property; propertyShort = `__${propertyShort}`; - let cls = `${propertyShort}-${JSON.stringify(value)}`; + let cls = `${propertyShort}-${JSON.stringify(fastn_dom.class_count)}`; if (!!className) { cls = className; } else { @@ -1234,7 +1236,7 @@ class Node2 { this.attachCss("align-items", "end"); break; case 'bottom-center': - this.attachCss("justify-content", "start"); + this.attachCss("justify-content", "center"); this.attachCss("align-items", "end"); break; case 'bottom-right': @@ -1244,6 +1246,58 @@ class Node2 { } } } + attachLinkColor(value) { + ftd.dark_mode.addClosure(fastn.closure(() => { + if (!ssr) { + const anchors = this.#node.tagName.toLowerCase() === 'a' + ? [this.#node] + : Array.from(this.#node.querySelectorAll("a")); + let propertyShort = `__${fastn_dom.propertyMap["link-color"]}`; + + if(fastn_utils.isNull(value)) { + anchors.forEach(a => { + a.classList.values().forEach(className => { + if(className.startsWith(`${propertyShort}-`)) { + a.classList.remove(className); + } + }); + }); + } else { + const lightValue = fastn_utils.getStaticValue(value.get("light")); + const darkValue = fastn_utils.getStaticValue(value.get("dark")); + let cls = `${propertyShort}-${JSON.stringify(lightValue)}`; + + if (!fastn_dom.unsanitised_classes[cls]) { + fastn_dom.unsanitised_classes[cls] = ++fastn_dom.class_count; + } + + cls = `${propertyShort}-${fastn_dom.unsanitised_classes[cls]}`; + + const cssClass = `.${cls}`; + + if (!fastn_dom.classes[cssClass]) { + const obj = { property: "color", value: lightValue }; + fastn_dom.classes[cssClass] = fastn_dom.classes[cssClass] || obj; + let styles = document.getElementById('styles'); + styles.innerHTML = `${styles.innerHTML}${getClassAsString(cssClass, obj)}\n`; + } + + if(lightValue !== darkValue) { + const obj = { property: "color", value: darkValue }; + let darkCls = `body.dark ${cssClass}`; + if (!fastn_dom.classes[darkCls]) { + fastn_dom.classes[darkCls] = fastn_dom.classes[darkCls] || obj; + let styles = document.getElementById('styles'); + styles.innerHTML = `${styles.innerHTML}${getClassAsString(darkCls, obj)}\n`; + } + } + + anchors.forEach(a => a.classList.add(cls)); + } + } + }).addNodeProperty(this, null, inherited)); + this.#mutables.push(ftd.dark_mode); + } setStaticProperty(kind, value, inherited) { // value can be either static or mutable let staticValue = fastn_utils.getStaticValue(value); @@ -1494,6 +1548,8 @@ class Node2 { this.attachColorCss("border-top-color", staticValue); } else if (kind === fastn_dom.PropertyKind.BorderBottomColor) { this.attachColorCss("border-bottom-color", staticValue); + } else if (kind === fastn_dom.PropertyKind.LinkColor) { + this.attachLinkColor(staticValue); } else if (kind === fastn_dom.PropertyKind.Color) { this.attachColorCss("color", staticValue, true); } else if (kind === fastn_dom.PropertyKind.Background) { diff --git a/fastn-js/js/fastn.js b/fastn-js/js/fastn.js index 07dbe6d19a..fdb07bc50f 100644 --- a/fastn-js/js/fastn.js +++ b/fastn-js/js/fastn.js @@ -266,7 +266,7 @@ class MutableList { let current_list = this.#list; let new_list = []; for (let idx in current_list) { - new_list.push( { item: fastn_utils.clone(current_list[idx].item), index: new Mutable(parseInt(idx)) }); + new_list.push(fastn_utils.clone(current_list[idx].item)); } return new MutableList(new_list); } diff --git a/fastn-js/src/property.rs b/fastn-js/src/property.rs index 78d1d940b5..0ce8c31966 100644 --- a/fastn-js/src/property.rs +++ b/fastn-js/src/property.rs @@ -318,6 +318,7 @@ pub enum PropertyKind { Region, OpenInNewTab, Link, + LinkColor, LinkRel, Anchor, Classes, @@ -432,6 +433,7 @@ impl PropertyKind { PropertyKind::Id => "fastn_dom.PropertyKind.Id", PropertyKind::Css => "fastn_dom.PropertyKind.Css", PropertyKind::Js => "fastn_dom.PropertyKind.Js", + PropertyKind::LinkColor => "fastn_dom.PropertyKind.LinkColor", PropertyKind::LinkRel => "fastn_dom.PropertyKind.LinkRel", PropertyKind::AlignSelf => "fastn_dom.PropertyKind.AlignSelf", PropertyKind::Anchor => "fastn_dom.PropertyKind.Anchor", diff --git a/fastn-js/src/to_js.rs b/fastn-js/src/to_js.rs index 4c48967c35..1d58968bba 100644 --- a/fastn-js/src/to_js.rs +++ b/fastn-js/src/to_js.rs @@ -943,6 +943,9 @@ impl ExpressionGenerator { return format!( indoc::indoc! { "let fastn_utils_val_{refined_var} = fastn_utils.clone({val}); + if (fastn_utils_val_{refined_var} instanceof fastn.mutableClass) {{ + fastn_utils_val_{refined_var} = fastn_utils_val_{refined_var}.get(); + }} if (!fastn_utils.setter({var}, fastn_utils_val_{refined_var})) {{ {var} = fastn_utils_val_{refined_var}; }}" diff --git a/fastn/Cargo.toml b/fastn/Cargo.toml index e71a05a61f..3f2ba5399f 100644 --- a/fastn/Cargo.toml +++ b/fastn/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "fastn" -version = "0.3.42" +version = "0.3.49" authors.workspace = true edition.workspace = true license.workspace = true diff --git a/ftd/src/interpreter/things/default.rs b/ftd/src/interpreter/things/default.rs index fa3ad63ed3..f46de6eac2 100644 --- a/ftd/src/interpreter/things/default.rs +++ b/ftd/src/interpreter/things/default.rs @@ -10761,6 +10761,12 @@ fn text_arguments() -> Vec { .into_optional() .into_kind_data(), ), + ftd::interpreter::Argument::default( + "link-color", + ftd::interpreter::Kind::record(ftd::interpreter::FTD_COLOR) + .into_optional() + .into_kind_data(), + ), ] } diff --git a/ftd/src/js/element.rs b/ftd/src/js/element.rs index 9448ec1d87..af6987c1ce 100644 --- a/ftd/src/js/element.rs +++ b/ftd/src/js/element.rs @@ -1888,6 +1888,7 @@ pub struct TextCommon { pub line_clamp: Option, pub style: Option, pub display: Option, + pub link_color: Option, } impl TextCommon { @@ -1910,6 +1911,7 @@ impl TextCommon { line_clamp: ftd::js::value::get_optional_js_value("line-clamp", properties, arguments), style: ftd::js::value::get_optional_js_value("style", properties, arguments), display: ftd::js::value::get_optional_js_value("display", properties, arguments), + link_color: ftd::js::value::get_optional_js_value("link-color", properties, arguments), } } @@ -1960,6 +1962,16 @@ impl TextCommon { display.to_set_property(fastn_js::PropertyKind::Display, doc, element_name, rdata), )); } + if let Some(ref link_color) = self.link_color { + component_statements.push(fastn_js::ComponentStatement::SetProperty( + link_color.to_set_property( + fastn_js::PropertyKind::LinkColor, + doc, + element_name, + rdata, + ), + )); + } component_statements } } diff --git a/ftd/t/js/01-basic-module.html b/ftd/t/js/01-basic-module.html index c2f051f5af..9501b33469 100644 --- a/ftd/t/js/01-basic-module.html +++ b/ftd/t/js/01-basic-module.html @@ -16,20 +16,20 @@ -
fastn
Hello
Hello
+ + + + + + +
Rithik
Ritesh
Heulitig
+ + diff --git a/ftd/t/js/53-link-color.ftd b/ftd/t/js/53-link-color.ftd new file mode 100644 index 0000000000..e944c47d31 --- /dev/null +++ b/ftd/t/js/53-link-color.ftd @@ -0,0 +1,14 @@ +-- ftd.color link-color: +dark: red +light: blue + +-- ftd.text: Click me +link: https://google.com +link-color: $link-color + +-- ftd.text: +link-color: $link-color + +Hello world [Test](https://google.com) + +This is awesome [Test](https://google.com) diff --git a/ftd/t/js/53-link-color.html b/ftd/t/js/53-link-color.html new file mode 100644 index 0000000000..a2126a6d6f --- /dev/null +++ b/ftd/t/js/53-link-color.html @@ -0,0 +1,90 @@ + + + + + + + + + + + + + + +
Click me
Hello world [Test](https://google.com) + +This is awesome [Test](https://google.com)
+ + diff --git a/ftd/t/js/54-class-fix.ftd b/ftd/t/js/54-class-fix.ftd new file mode 100644 index 0000000000..c86a68bba9 --- /dev/null +++ b/ftd/t/js/54-class-fix.ftd @@ -0,0 +1,10 @@ +-- ftd.text: Hello +background.solid: $bg-yg + +-- ftd.text: hello +background.solid: yellow + + +-- ftd.color bg-yg: +light: yellow +dark: green diff --git a/ftd/t/js/54-class-fix.html b/ftd/t/js/54-class-fix.html new file mode 100644 index 0000000000..36b2d923e1 --- /dev/null +++ b/ftd/t/js/54-class-fix.html @@ -0,0 +1,93 @@ + + + + + + + + + + + + + + +
Hello
hello
+ +