mirror of
https://github.com/Nutomic/ibis.git
synced 2025-01-25 23:05:49 +00:00
Store editor preview preference in cookie
This commit is contained in:
parent
96233ba52a
commit
793810f737
3 changed files with 21 additions and 15 deletions
|
@ -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::{ev::beforeunload, html::Textarea, prelude::*};
|
||||||
use leptos_use::{use_event_listener, use_window};
|
use leptos_use::{use_event_listener, use_window};
|
||||||
|
|
||||||
|
@ -9,7 +9,8 @@ pub fn EditorView(
|
||||||
set_content: WriteSignal<String>,
|
set_content: WriteSignal<String>,
|
||||||
) -> impl IntoView {
|
) -> impl IntoView {
|
||||||
let (preview, set_preview) = signal(render_markdown(&content.get_untracked()));
|
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
|
// Prevent user from accidentally closing the page while editing. Doesnt prevent navigation
|
||||||
// within Ibis.
|
// within Ibis.
|
||||||
|
@ -44,7 +45,9 @@ pub fn EditorView(
|
||||||
<div class="flex items-center mb-4 h-min">
|
<div class="flex items-center mb-4 h-min">
|
||||||
<button
|
<button
|
||||||
class="btn btn-secondary"
|
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
|
Preview
|
||||||
</button>
|
</button>
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
use chrono::{Duration, Local};
|
use super::use_cookie;
|
||||||
use codee::string::FromToStringCodec;
|
|
||||||
use leptos::prelude::*;
|
use leptos::prelude::*;
|
||||||
use leptos_use::{use_cookie_with_options, use_preferred_dark, SameSite, UseCookieOptions};
|
use leptos_use::use_preferred_dark;
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct DarkMode {
|
pub struct DarkMode {
|
||||||
|
@ -12,14 +11,7 @@ pub struct DarkMode {
|
||||||
|
|
||||||
impl DarkMode {
|
impl DarkMode {
|
||||||
pub fn init() -> Self {
|
pub fn init() -> Self {
|
||||||
let expires = (Local::now() + Duration::days(356)).timestamp();
|
let cookie = use_cookie("dark_mode");
|
||||||
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 is_dark = Signal::derive(move || {
|
let is_dark = Signal::derive(move || {
|
||||||
let default = || use_preferred_dark().get_untracked();
|
let default = || use_preferred_dark().get_untracked();
|
||||||
cookie.0.get().unwrap_or_else(default)
|
cookie.0.get().unwrap_or_else(default)
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
use crate::common::{utils::extract_domain, DbArticle, DbPerson};
|
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::prelude::*;
|
||||||
|
use leptos_use::{use_cookie_with_options, SameSite, UseCookieOptions};
|
||||||
|
|
||||||
pub mod api;
|
pub mod api;
|
||||||
pub mod app;
|
pub mod app;
|
||||||
|
@ -79,3 +81,12 @@ fn render_date_time(date_time: DateTime<Utc>) -> String {
|
||||||
.format("%Y-%m-%d %H:%M:%S")
|
.format("%Y-%m-%d %H:%M:%S")
|
||||||
.to_string()
|
.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)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue