diff --git a/Cargo.lock b/Cargo.lock index 8274f5a..f15ca03 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -838,7 +838,7 @@ dependencies = [ ] [[package]] -name = "mdwiki" +name = "mdnotes" version = "0.1.0" dependencies = [ "actix-files", diff --git a/Cargo.toml b/Cargo.toml index 2e80a14..acd9160 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "mdwiki" +name = "mdnotes" version = "0.1.0" edition = "2021" diff --git a/README.md b/README.md index 424623a..78a0d34 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ -mdwiki +mdnotes ====== -Wiki utilisant la syntaxe MarkDown pour être rapide et facile à modifier. +Notes utilisant la syntaxe MarkDown pour être rapide et facile à modifier. Idéal pour la prise de notes. Création dynamique de pages en créant un lien dans une page. diff --git a/src/commons.rs b/src/commons.rs index f1d69a9..bf8b26e 100644 --- a/src/commons.rs +++ b/src/commons.rs @@ -1,6 +1,5 @@ use clap::Parser; - #[derive(Parser)] pub struct Arguments { /// IP address diff --git a/src/main.rs b/src/main.rs index eb624d2..92ebb75 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,8 @@ use actix_web::{post, web, App, HttpResponse, HttpServer, Responder}; use actix_files; -use mdwiki::commons::{Arguments, AppData}; -use mdwiki::index; +use mdnotes::commons::{Arguments, AppData}; +use mdnotes::index; #[actix_web::main] async fn main() -> std::io::Result<()> { @@ -21,6 +21,7 @@ async fn main() -> std::io::Result<()> { .app_data(web::Data::new(appdata.to_owned())) .service(index::index) .service(actix_files::Files::new("/static", "./static")) + .service(actix_files::Files::new("/static/modules", "./static/modules")) }) .bind((ip, port))? .run() diff --git a/static/mdnotes.css b/static/mdnotes.css index 37531fd..f945efe 100644 --- a/static/mdnotes.css +++ b/static/mdnotes.css @@ -1,9 +1,13 @@ html { - background-color: #f5f5f5; + font-family: system-ui; + font-size: 12pt; +} + +body { + margin: 0; } div#mdnotes { - font-family: sans-serif; padding-top: 1rem; padding-left: 3rem; padding-right: 3rem; @@ -11,29 +15,91 @@ div#mdnotes { background-color: #ffffff; } +div#mdnotes:focus { + outline: none; +} + div#mdnotes div.h1 { - font-size: 2rem; - font-weight: bold; + font-size: 3rem; margin-top: 1rem; margin-bottom: 1.5rem; border-bottom: 1px solid #666666; } div#mdnotes div.h2 { - font-size: 1.5rem; - font-weight: bold; + font-size: 2.2rem; margin-top: 0.5rem; margin-bottom: 1rem; } div#mdnotes div.h3 { - font-size: 1rem; - font-weight: bold; - margin-top: 0.3rem; - margin-bottom: 0.5rem; + font-size: 1.5rem; + margin-top: 0.5rem; + margin-bottom: 1rem; } div#mdnotes div.h4 { font-style: italic; - margin-bottom: 0.3rem; + margin-top: 0.5rem; + margin-bottom: 0.5rem; +} + +div#mdnotes div.bq1 { + font-style: italic; + padding-top: 0.3rem; + padding-bottom: 0.3rem; + padding-left: .9rem; + background-color: #f5f5f5; + border-left: .3rem solid #446699; +} + +div#mdnotes div.bq2 { + font-style: italic; + padding-top: 0.3rem; + padding-bottom: 0.3rem; + padding-left: .6rem; + background-color: #f5f5f5; + border-left: .6rem solid #5577aa; +} + +div#mdnotes div.bq3 { + font-style: italic; + padding-top: 0.3rem; + padding-bottom: 0.3rem; + padding-left: .3rem; + background-color: #f5f5f5; + border-left: .9rem solid #6688bb; +} + + +div#mdnotes div.ul { + padding-left: 1rem; +} + +div#mdnotes div.mdnotes_line span.token { + font-weight: 1; + color: #88aaff; + overflow: hidden; +} + +div#mdnotes div.mdnotes_line span.token { + font-size: 0; +} + +div#mdnotes:focus div.mdnotes_line span.token { + font-size: inherit; +} + +div#mdnotes div.body { + margin-top: 0.5rem; + margin-bottom: 0.5rem; +} + +div#mdnotes span.bold { + font-weight: bold; +} + +div#mdnotes span.mono { + font-family: monospace; + color: #885511; } \ No newline at end of file diff --git a/static/mdnotes.js b/static/mdnotes.js index 2118908..4061d09 100644 --- a/static/mdnotes.js +++ b/static/mdnotes.js @@ -17,6 +17,11 @@ function change(evt) { function reId(obj) { let prevId = ''; let children = obj.childNodes; + if (children.length == 1 && getType(children[0]) == 'TEXT') { + obj.innerHTML = '
' + obj.innerText + '
'; + setStartPositionInLine(document.getElementById('newline'), 1); + children = obj.childNodes; + } for (i = 0; i < children.length; i++) { let child = children[i]; if (child.classList.contains('mdnotes_line')) { diff --git a/static/modules/md.js b/static/modules/md.js new file mode 100644 index 0000000..98d3c78 --- /dev/null +++ b/static/modules/md.js @@ -0,0 +1,65 @@ +function load(textarea, div) { + let lines = textarea.value.split('\n'); + for (let i = 0; i < lines.length; i++) { + let line = lines[i]; + let elem = formatLine(line); + elem.id = 'mdnotes_' + i; + div.append(elem); + } +} + +function formatLine(line) { + let normLine = line.trimStart(); + let styleClass = null; + let token = null; + if (normLine.match(/####\s/i)) { + token = /^(####\s)/i; + styleClass = 'h4'; + } else if (normLine.match(/###\s/i)) { + token = /^(###\s)/i; + styleClass = 'h3'; + } else if (normLine.match(/##\s/i)) { + token = /^(##\s)/i; + styleClass = 'h2'; + } else if (normLine.match(/#\s/i)) { + token = /^(#\s)/i; + styleClass = 'h1'; + } else if (normLine.match(/>>>\s/i)) { + token = /^(>>>\s)/i; + styleClass = 'bq3'; + } else if (normLine.match(/>>\s/i)) { + token = /^(>>\s)/i; + styleClass = 'bq2'; + } else if (normLine.match(/>\s/i)) { + token = /^(>\s)/i; + styleClass = 'bq1'; + } else if (normLine.match(/^[*-]\s/i)) { + styleClass = 'ul'; + } else { + styleClass = 'body'; + } + let elem = document.createElement('div'); + elem.classList.add('mdnotes_line'); + elem.classList.add(styleClass); + normLine = addBold(normLine); + normLine = addMono(normLine); + if (token != null) { + elem.classList.add('hastoken'); + normLine = normLine.replace(token, '$1'); + } + elem.innerHTML = normLine; + return elem; +} + +function addMono(line) { + line = line.replace(/`(.*?)`/ig, '`$1`'); + return line; +} + +function addBold(line) { + line = line.replace(/\*\*([^\s].*?)\*\*/ig, '**$1**'); + line = line.replace(/__([^\s].*?)__/ig, '__$1__'); + return line; +} + +export { load, formatLine }; \ No newline at end of file diff --git a/static/modules/mdnotes.js b/static/modules/mdnotes.js new file mode 100644 index 0000000..20a4c74 --- /dev/null +++ b/static/modules/mdnotes.js @@ -0,0 +1,28 @@ +import { getStartPositionInLine, setStartPositionInLine } from './position.js'; +import { formatLine, load } from './md.js'; + +function onedit(e) { + let ret = getStartPositionInLine(); + let line = ret[0]; + let position = ret[1]; + + if (line.innerText == '\n') { + line.className = 'mdnotes_line'; + return; + } + + let newline = formatLine(line.innerText); + line.parentNode.replaceChild(newline, line); + setStartPositionInLine(newline, position); +} + +function init() { + let mdnotesdiv = document.getElementById('mdnotes'); + mdnotesdiv.addEventListener('input', onedit); + + load(document.getElementById('ta'), mdnotesdiv); +} + +export { onedit }; + +init(); \ No newline at end of file diff --git a/static/modules/position.js b/static/modules/position.js new file mode 100644 index 0000000..6208190 --- /dev/null +++ b/static/modules/position.js @@ -0,0 +1,74 @@ +function getStartPositionInLine() { + let selection = window.getSelection(); + if (selection.rangeCount == 0) { + return [null, 0]; + } + let range = selection.getRangeAt(0).cloneRange(); + let obj = range.startContainer; + + let position = range.startOffset; + + if (obj.nodeName == 'DIV' && obj.classList.contains('mdnotes_line')) { + return [obj, position]; + } + + let line = null; + + while (obj != null) { + if (obj.previousSibling != null) { + obj = obj.previousSibling; + if (obj.innerText != null) { + position += obj.innerText.length; + } else if (obj.length != null) { + position += obj.length; + } + } else if (obj.parentNode != null && obj.parentNode.classList != null && !obj.parentNode.classList.contains('mdnotes_line')) { + obj = obj.parentNode; + } else if (obj.parentNode != null && obj.parentNode.classList != null && obj.parentNode.classList.contains('mdnotes_line')) { + line = obj.parentNode; + obj = null; + } else { + obj = null; + } + } + return [line, position]; +} + +function setStartPositionInLine(line, position) { + setStartPositionInDiv(line, position) +} + +function setStartPositionInDiv(obj, position) { + let temppos = position; + if (temppos > obj.innerText.length) { + return temppos - obj.innerText.length; + } + let children = obj.childNodes; + for (let i = 0; i < children.length; i++) { + let child = children[i]; + if (child.nodeType == 3) { + temppos = setStartPositionInText(child, temppos); + } else { + temppos = setStartPositionInDiv(child, temppos); + } + if (temppos == 0) { + return temppos; + } + } + return temppos; +} + +function setStartPositionInText(obj, position) { + if (position > obj.length) { + return position - obj.length; + } + + let selection = window.getSelection(); + selection.removeAllRanges(); + let range = document.createRange(); + range.setStart(obj, position); + selection.addRange(range); + return 0; +} + +export { getStartPositionInLine, setStartPositionInLine }; \ No newline at end of file diff --git a/templates/index.html b/templates/index.html index 7aee816..05e1da2 100644 --- a/templates/index.html +++ b/templates/index.html @@ -1,5 +1,12 @@ + + + + + + + {{name}} @@ -13,8 +20,11 @@ Encore un joli texte ### Titre 3-2 T'en as marre ??? #### Titre 4 + - coucou + - pas vrai + - tant pis Moi oui !!! -
- +
+