1
0
Fork 0
mirror of https://github.com/Nutomic/ibis.git synced 2025-01-11 10:15:48 +00:00

Store editor preview preference in cookie

This commit is contained in:
Felix Ableitner 2024-12-17 15:05:39 +01:00
parent 96233ba52a
commit 793810f737
3 changed files with 21 additions and 15 deletions

View file

@ -1,4 +1,4 @@
use crate::frontend::markdown::render_markdown;
use crate::frontend::{markdown::render_markdown, use_cookie};
use leptos::{ev::beforeunload, html::Textarea, prelude::*};
use leptos_use::{use_event_listener, use_window};
@ -9,7 +9,8 @@ pub fn EditorView(
set_content: WriteSignal<String>,
) -> impl IntoView {
let (preview, set_preview) = signal(render_markdown(&content.get_untracked()));
let (show_preview, set_show_preview) = signal(false);
let cookie = use_cookie("editor_preview");
let show_preview = Signal::derive(move || cookie.0.get().unwrap_or_else(|| true));
// Prevent user from accidentally closing the page while editing. Doesnt prevent navigation
// within Ibis.
@ -44,7 +45,9 @@ pub fn EditorView(
<div class="flex items-center mb-4 h-min">
<button
class="btn btn-secondary"
on:click=move |_| { set_show_preview.update(|s| *s = !*s) }
on:click=move |_| {
cookie.1.set(Some(!show_preview.get_untracked()));
}
>
Preview
</button>

View file

@ -1,7 +1,6 @@
use chrono::{Duration, Local};
use codee::string::FromToStringCodec;
use super::use_cookie;
use leptos::prelude::*;
use leptos_use::{use_cookie_with_options, use_preferred_dark, SameSite, UseCookieOptions};
use leptos_use::use_preferred_dark;
#[derive(Debug, Clone)]
pub struct DarkMode {
@ -12,14 +11,7 @@ pub struct DarkMode {
impl DarkMode {
pub fn init() -> Self {
let expires = (Local::now() + Duration::days(356)).timestamp();
let cookie_options = UseCookieOptions::default()
.path("/")
.expires(expires)
.same_site(SameSite::Strict);
let cookie =
use_cookie_with_options::<bool, FromToStringCodec>("dark_mode", cookie_options);
let cookie = use_cookie("dark_mode");
let is_dark = Signal::derive(move || {
let default = || use_preferred_dark().get_untracked();
cookie.0.get().unwrap_or_else(default)

View file

@ -1,6 +1,8 @@
use crate::common::{utils::extract_domain, DbArticle, DbPerson};
use chrono::{DateTime, Local, Utc};
use chrono::{DateTime, Duration, Local, Utc};
use codee::string::FromToStringCodec;
use leptos::prelude::*;
use leptos_use::{use_cookie_with_options, SameSite, UseCookieOptions};
pub mod api;
pub mod app;
@ -79,3 +81,12 @@ fn render_date_time(date_time: DateTime<Utc>) -> String {
.format("%Y-%m-%d %H:%M:%S")
.to_string()
}
fn use_cookie(name: &str) -> (Signal<Option<bool>>, WriteSignal<Option<bool>>) {
let expires = (Local::now() + Duration::days(356)).timestamp();
let cookie_options = UseCookieOptions::default()
.path("/")
.expires(expires)
.same_site(SameSite::Strict);
use_cookie_with_options::<bool, FromToStringCodec>(name, cookie_options)
}