From 161b3e37550bb664f049b740954f96830fe11f45 Mon Sep 17 00:00:00 2001 From: Skylar Ittner Date: Sat, 24 Nov 2018 14:25:43 -0700 Subject: [PATCH] Add AJAX note refresh, note text now changes color to stay readable on dark backgrounds --- action.php | 8 ++++++++ lib/Note.lib.php | 25 +++++++++++++++++++++++++ pages.php | 5 ++++- pages/home.php | 15 ++++++++------- static/js/home.js | 26 ++++++++++++++++++++++++++ 5 files changed, 71 insertions(+), 8 deletions(-) create mode 100644 static/js/home.js diff --git a/action.php b/action.php index 35693c3..ab4a272 100644 --- a/action.php +++ b/action.php @@ -82,4 +82,12 @@ switch ($VARS['action']) { $note->saveNote(); returnToSender(""); break; + case "getnotes": + header("Content-Type: application/json"); + $noteids = $database->select('notes', 'noteid', ['ownerid' => $_SESSION['uid']]); + $notes = []; + foreach ($noteids as $n) { + $notes[] = Note::loadNote($n)->toArray(); + } + exit(json_encode($notes)); } \ No newline at end of file diff --git a/lib/Note.lib.php b/lib/Note.lib.php index 16d1085..45743cf 100644 --- a/lib/Note.lib.php +++ b/lib/Note.lib.php @@ -202,6 +202,30 @@ class Note { return $this->favorite; } + /** + * Get the text color for this note, based on the background color. + * Thanks to https://stackoverflow.com/a/8468448 + * @return string + */ + public function getTextColor(): string { + $bg = $this->getColor(); + $r = hexdec(substr($bg, 0, 2)); + $g = hexdec(substr($bg, 2, 2)); + $b = hexdec(substr($bg, 4, 2)); + + $contrast = sqrt( + $r * $r * .241 + + $g * $g * .691 + + $b * $b * .068 + ); + + if ($contrast > 130) { + return '000000'; + } else { + return 'FFFFFF'; + } + } + /** * Set the note content * @param string $markdown @@ -272,6 +296,7 @@ class Note { 'noteid' => $this->getID(), 'color' => $this->getColor(), 'content' => $this->getText(), + 'html' => $this->getHTML(true), 'title' => $this->getTitle(), 'modified' => $this->getModified(), 'favorite' => $this->getFavorite(), diff --git a/pages.php b/pages.php index 6419dca..f7eedf6 100644 --- a/pages.php +++ b/pages.php @@ -9,7 +9,10 @@ define("PAGES", [ "home" => [ "title" => "Notes", "navbar" => true, - "icon" => "far fa-sticky-note" + "icon" => "far fa-sticky-note", + "scripts" => [ + "static/js/home.js" + ] ], "404" => [ "title" => "404 error" diff --git a/pages/home.php b/pages/home.php index 702fe23..0c0f6ab 100644 --- a/pages/home.php +++ b/pages/home.php @@ -15,8 +15,9 @@ foreach ($noteids as $n) {