Skip to content

Commit

Permalink
fix windows cache dir
Browse files Browse the repository at this point in the history
  • Loading branch information
harshdoesdev committed Sep 11, 2023
1 parent 82e1c13 commit 99c7438
Show file tree
Hide file tree
Showing 53 changed files with 1,890 additions and 442 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion fastn-core/src/package/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,14 @@ impl Package {
package.redirects = {
let redirects_temp: Option<redirects::RedirectsTemp> =
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
Expand Down
51 changes: 47 additions & 4 deletions fastn-core/src/package/redirects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,62 @@ pub struct RedirectsTemp {
}

impl RedirectsTemp {
pub(crate) fn redirects_from_body(&self) -> ftd::Map<String> {
pub(crate) fn redirects_from_body(&self) -> fastn_core::Result<ftd::Map<String>> {
let body = self.body.as_str();
let mut redirects: ftd::Map<String> = 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
// <some link>: <link to redirect>
// <some link> -> <link to redirect>

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<String>,
) -> 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(())
}
}

Expand Down
13 changes: 11 additions & 2 deletions fastn-core/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,18 @@ pub fn get_ftd_hash(path: &str) -> fastn_core::Result<String> {
}

pub fn get_cache_file(id: &str) -> Option<std::path::PathBuf> {
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")
Expand Down
60 changes: 58 additions & 2 deletions fastn-js/js/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -273,6 +274,7 @@ fastn_dom.PropertyKind = {
LoopVideo: 113,
Controls: 114,
Muted: 115,
LinkColor: 116,
};


Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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':
Expand All @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion fastn-js/js/fastn.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 2 additions & 0 deletions fastn-js/src/property.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ pub enum PropertyKind {
Region,
OpenInNewTab,
Link,
LinkColor,
LinkRel,
Anchor,
Classes,
Expand Down Expand Up @@ -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",
Expand Down
3 changes: 3 additions & 0 deletions fastn-js/src/to_js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
}}"
Expand Down
2 changes: 1 addition & 1 deletion fastn/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "fastn"
version = "0.3.42"
version = "0.3.49"
authors.workspace = true
edition.workspace = true
license.workspace = true
Expand Down
6 changes: 6 additions & 0 deletions ftd/src/interpreter/things/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10761,6 +10761,12 @@ fn text_arguments() -> Vec<ftd::interpreter::Argument> {
.into_optional()
.into_kind_data(),
),
ftd::interpreter::Argument::default(
"link-color",
ftd::interpreter::Kind::record(ftd::interpreter::FTD_COLOR)
.into_optional()
.into_kind_data(),
),
]
}

Expand Down
12 changes: 12 additions & 0 deletions ftd/src/js/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1888,6 +1888,7 @@ pub struct TextCommon {
pub line_clamp: Option<ftd::js::Value>,
pub style: Option<ftd::js::Value>,
pub display: Option<ftd::js::Value>,
pub link_color: Option<ftd::js::Value>,
}

impl TextCommon {
Expand All @@ -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),
}
}

Expand Down Expand Up @@ -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
}
}
Expand Down
22 changes: 11 additions & 11 deletions ftd/t/js/01-basic-module.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions ftd/t/js/02-property.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 99c7438

Please sign in to comment.