The scripts of Markdown Notes
Version 1.0. These are the scripts exactly as they are in the stack that plays here. Each line has its number, and a link to a line keeps it.
34 scripts in this stack
- card id 1002 (card 1) Markdown Notes, 768 lines
- button "View script" (card 1) Markdown Notes, 9 lines
- button "New note" (card 1) Markdown Notes, 5 lines
- graphic "rowBack1" (card 1) Markdown Notes, 2 lines
- graphic "rowBack2" (card 1) Markdown Notes, 2 lines
- graphic "rowBack3" (card 1) Markdown Notes, 2 lines
- graphic "rowBack4" (card 1) Markdown Notes, 2 lines
- graphic "rowBack5" (card 1) Markdown Notes, 2 lines
- graphic "rowBack6" (card 1) Markdown Notes, 2 lines
- graphic "rowBack7" (card 1) Markdown Notes, 2 lines
- graphic "rowBack8" (card 1) Markdown Notes, 2 lines
- button "Tool H" (card 1) Markdown Notes, 5 lines
- button "Tool B" (card 1) Markdown Notes, 5 lines
- button "Tool I" (card 1) Markdown Notes, 5 lines
- button "Tool List" (card 1) Markdown Notes, 5 lines
- button "Tool Task" (card 1) Markdown Notes, 5 lines
- button "Tool Code" (card 1) Markdown Notes, 5 lines
- button "Tool Link" (card 1) Markdown Notes, 5 lines
- button "Delete" (card 1) Markdown Notes, 5 lines
- field "Editor" (card 1) Markdown Notes, 5 lines
- button "Get md" (card 1) Markdown Notes, 5 lines
- button "Get html" (card 1) Markdown Notes, 5 lines
- field "Preview" (card 1) Markdown Notes, 2 lines
- graphic "EditThumb" (card 1) Markdown Notes, 2 lines
- graphic "ViewThumb" (card 1) Markdown Notes, 2 lines
- the stack script Markdown Notes Script Viewer, 28 lines
- card "Viewer" (card 1) Markdown Notes Script Viewer, 369 lines
- field "svObjects" (card 1) Markdown Notes Script Viewer, 5 lines
- button "svOnly" (card 1) Markdown Notes Script Viewer, 5 lines
- field "svCode" (card 1) Markdown Notes Script Viewer, 5 lines
- button "svCopy" (card 1) Markdown Notes Script Viewer, 4 lines
- button "svShowMe" (card 1) Markdown Notes Script Viewer, 4 lines
- button "svEdit" (card 1) Markdown Notes Script Viewer, 5 lines
- button "svClose" (card 1) Markdown Notes Script Viewer, 4 lines
card id 1002 (card 1)
Markdown Notes.oxtstack, stack "Markdown Notes", 768 lines
-- Markdown Notes: write Markdown on the left, see it styled on the right. -- -- The preview is an ordinary field. As you type, the card script reads -- your note line by line, turns the Markdown into the field's htmlText -- (headings, lists, ticks, quotes, code, links), and sets it. The same -- reader writes real HTML when you download the note as a web page. -- Notes are kept in a file: in a browser, in the page's own storage. constant kFile = "markdown-notes.dat" constant kMax = 8 constant kAccent = "#2563EB" local sNotes, sIds, sCur, sNextId, sRenderTimer, sSaveTimer, sToastTimer, sMap on preOpenCard mdLoad if sIds is empty then mdFirstNotes mdOpen item 1 of sIds end preOpenCard on closeCard if sRenderTimer is not empty then cancel sRenderTimer if sSaveTimer is not empty then cancel sSaveTimer mdSave end if if sToastTimer is not empty then cancel sToastTimer put empty into sRenderTimer put empty into sSaveTimer put empty into sToastTimer end closeCard -- Where the notes are kept. In a browser, the documents folder is the -- page's own storage, kept between visits. function mdPath local tFolder if the platform is "HTML5" then put specialFolderPath("documents") into tFolder else put specialFolderPath("temporary") & "/oxt-notes" into tFolder end if if there is not a folder tFolder then create folder tFolder return tFolder & "/" & kFile end mdPath command mdLoad local tData, tA put empty into sNotes put empty into sIds put 0 into sNextId put url ("binfile:" & mdPath()) into tData if tData is empty then exit mdLoad try put arrayDecode(tData) into tA catch tErr exit mdLoad end try put tA["notes"] into sNotes put tA["order"] into sIds put tA["next"] into sNextId if sNextId is not an integer then put 0 into sNextId end mdLoad command mdSave local tA put empty into sSaveTimer put sNotes into tA["notes"] put sIds into tA["order"] put sNextId into tA["next"] put arrayEncode(tA) into url ("binfile:" & mdPath()) end mdSave command mdSaveSoon if sSaveTimer is not empty then cancel sSaveTimer send "mdSave" to me in 600 milliseconds put the result into sSaveTimer end mdSaveSoon command mdAdd pText, pTime add 1 to sNextId put pText into sNotes[sNextId]["text"] put pTime into sNotes[sNextId]["time"] if sIds is empty then put sNextId into sIds else put sNextId & comma before sIds end if return sNextId end mdAdd command mdFirstNotes local tT put "# Ideas for the web IDE" & return & return & \ "1. A gallery of examples on the home page" & return & \ "2. Open a stack straight from a link" & return & \ "3. Share a stack with **one click**" & return & return & \ "> The best ideas come from people who use it." into tT mdAdd tT, the seconds - 2 * 86400 put "# Shopping list" & return & return & \ "- [x] Coffee" & return & "- [ ] Bread" & return & "- [ ] Apples, *the green ones*" & return & "- [ ] Batteries for the remote" into tT mdAdd tT, the seconds - 3 * 3600 put "# Welcome to Markdown Notes" & return & \ "Write on the left. The right side shows your note **styled**, as you type." & return & return & \ "## What you can write" & return & \ "- **Bold** with two stars, *italic* with one" & return & \ "- `code` between backticks" & return & \ "- [Links](https://openxtalk.org) that open in a new tab" & return & \ "- ~~Mistakes~~ crossed out" & return & return & \ "## A list of things to do" & return & \ "- [x] Open this sample" & return & \ "- [ ] Click a box on the right to tick it" & return & \ "- [ ] Write a note of your own" & return & return & \ "> Every note is kept in this browser. The buttons above the preview download it as Markdown or as a web page." & return & return & \ "```" & return & "put " & quote & "Hello" & quote & " into field " & quote & "Greeting" & quote & return & "```" into tT mdAdd tT, the seconds mdSave end mdFirstNotes -- Shows a note command mdOpen pId if pId is empty then exit mdOpen put pId into sCur lock screen set the text of field "Editor" to sNotes[pId]["text"] set the vScroll of field "Editor" to 0 mdRender set the vScroll of field "Preview" to 0 mdShowList mdThumbs unlock screen end mdOpen on mdChanged put the text of field "Editor" into sNotes[sCur]["text"] put the seconds into sNotes[sCur]["time"] if item 1 of sIds is not sCur then mdDrop sCur put sCur & comma before sIds end if if sRenderTimer is not empty then cancel sRenderTimer send "mdRender" to me in 120 milliseconds put the result into sRenderTimer mdSaveSoon end mdChanged command mdDrop pId local tNew, tId repeat for each item tId in sIds if tId is not pId then put tId & comma after tNew end repeat delete the last char of tNew put tNew into sIds end mdDrop command mdRender local tScroll, tWords put empty into sRenderTimer put the vScroll of field "Preview" into tScroll lock screen set the htmlText of field "Preview" to mdHtml(sNotes[sCur]["text"], false) set the vScroll of field "Preview" to tScroll mdShowList mdThumbs unlock screen put the number of words of sNotes[sCur]["text"] into tWords set the text of field "Status" to tWords && mdPlural(tWords, "word") & mdDot() & max(1, round(tWords / 200)) && "min read" end mdRender function mdDot return " " & numToCodepoint(183) & " " end mdDot function mdPlural pCount, pWord if pCount is 1 then return pWord return pWord & "s" end mdPlural -- The list of notes, newest first command mdShowList local k, tId, tWords repeat with k = 1 to kMax put item k of sIds into tId set the visible of graphic ("rowBack" & k) to (tId is not empty) set the visible of field ("rowTitle" & k) to (tId is not empty) set the visible of field ("rowMeta" & k) to (tId is not empty) if tId is empty then next repeat mdFit "rowTitle" & k, mdTitle(sNotes[tId]["text"]) put the number of words of sNotes[tId]["text"] into tWords set the text of field ("rowMeta" & k) to mdAgo(sNotes[tId]["time"]) & mdDot() & tWords && mdPlural(tWords, "word") if tId is sCur then set the backgroundColor of graphic ("rowBack" & k) to "219,234,254" set the foregroundColor of field ("rowTitle" & k) to "29,78,216" else set the backgroundColor of graphic ("rowBack" & k) to "255,255,255" set the foregroundColor of field ("rowTitle" & k) to "30,41,59" end if end repeat end mdShowList -- Puts the words in the field, cut short with "..." when they do not fit command mdFit pField, pText set the text of field pField to pText if the formattedWidth of field pField <= the width of field pField then exit mdFit repeat while the formattedWidth of field pField > the width of field pField and the number of chars of pText > 1 delete the last char of pText set the text of field pField to word 1 to -1 of pText & "..." end repeat end mdFit -- A note's title: its first line with words, without the #s function mdTitle pText local tLine repeat for each line tLine in pText put word 1 to -1 of tLine into tLine repeat while char 1 of tLine is "#" delete char 1 of tLine end repeat put word 1 to -1 of tLine into tLine if tLine is not empty then return tLine end repeat return "Empty note" end mdTitle function mdAgo pTime local tAgo put the seconds - pTime into tAgo if tAgo < 60 then return "Just now" if tAgo < 3600 then return tAgo div 60 && "min ago" if tAgo < 86400 then return tAgo div 3600 && mdPlural(tAgo div 3600, "hour") && "ago" return tAgo div 86400 && mdPlural(tAgo div 86400, "day") && "ago" end mdAgo on mouseUp pButton local tName, tRow, tLine, tSource put the short name of the target into tName if tName begins with "row" then put char -1 of tName into tRow if item tRow of sIds is not empty and item tRow of sIds is not sCur then if sSaveTimer is not empty then cancel sSaveTimer mdSave end if mdOpen item tRow of sIds end if exit mouseUp end if if tName is "Preview" then -- a click on the box of a thing to do ticks it put word 2 of the clickLine into tLine if tLine is empty or sMap[tLine] is empty then exit mouseUp if the clickH > the left of field "Preview" + 48 then exit mouseUp put line sMap[tLine] of sNotes[sCur]["text"] into tSource if "[ ]" is in tSource then replace "[ ]" with "[x]" in tSource else if "[x]" is in tSource then replace "[x]" with "[ ]" in tSource else exit mouseUp end if put tSource into line sMap[tLine] of field "Editor" mdChanged exit mouseUp end if pass mouseUp end mouseUp -- A link in the preview opens in a new tab on linkClicked pLink if pLink is empty then exit linkClicked if the platform is "HTML5" then do "window.open(" & quote & mdJsString(pLink) & quote & ", '_blank', 'noopener')" as "JavaScript" else launch url pLink end if end linkClicked function mdJsString pText replace "\" with "\\" in pText replace quote with "\" & quote in pText return pText end mdJsString -- ----------------------------------------------------------------------- -- From Markdown: one line at a time. pWeb says whether the result is a -- web page (HTML) or the preview field's htmlText. -- ----------------------------------------------------------------------- function mdHtml pText, pWeb local tOut, tLine, tN, tP, tInCode, tCode, tList, tRest, tTag, tWord, tDone put 0 into tN put 0 into tP put false into tInCode if not pWeb then put empty into sMap repeat for each line tLine in pText add 1 to tN -- inside a block of code, every line is kept as it is if tInCode then if char 1 to 3 of word 1 of tLine is "```" then put false into tInCode add 1 to tP if pWeb then put "<pre><code>" & tCode & "</code></pre>" & return after tOut else put "<p bgcolor=" & quote & "#F1F5F9" & quote & " padding=" & quote & "8" & quote & " spacebelow=" & quote & "10" & quote & "><font color=" & quote & "#0F766E" & quote & ">" & tCode & "</font></p>" after tOut end if else if tCode is not empty then if pWeb then put return after tCode else put "" after tCode end if end if put mdEscape(tLine) after tCode end if next repeat end if put word 1 of tLine into tWord -- lists end at the first line that is not in them if tList is not empty and not mdIsItem(tLine) then if pWeb then put "</" & tList & ">" & return after tOut put empty into tList end if if char 1 to 3 of tWord is "```" then put true into tInCode put empty into tCode next repeat end if if word 1 to -1 of tLine is empty then next repeat add 1 to tP if not pWeb then put tN into sMap[tP] if tWord is "#" or tWord is "##" or tWord is "###" then put mdInline(word 2 to -1 of tLine, pWeb) into tRest if pWeb then put "<h" & length(tWord) & ">" & tRest & "</h" & length(tWord) & ">" & return after tOut else put "<p spaceabove=" & quote & "4" & quote & " spacebelow=" & quote & "8" & quote & "><b><font size=" & quote & item length(tWord) of "24,19,16" & quote & " color=" & quote & "#0F172A" & quote & ">" & tRest & "</font></b></p>" after tOut end if else if mdIsItem(tLine) then put mdItem(tLine, pWeb, tList) into tTag put line 1 of tTag into tList put line 2 to -1 of tTag after tOut else if tWord is ">" or char 1 of tWord is ">" then put tLine into tRest delete char 1 to offset(">", tRest) of tRest put mdInline(word 1 to -1 of tRest, pWeb) into tRest if pWeb then put "<blockquote>" & tRest & "</blockquote>" & return after tOut else put "<p bgcolor=" & quote & "#EFF6FF" & quote & " padding=" & quote & "8" & quote & " spacebelow=" & quote & "10" & quote & "><i><font color=" & quote & "#475569" & quote & ">" & tRest & "</font></i></p>" after tOut end if else if word 1 to -1 of tLine is "---" or word 1 to -1 of tLine is "***" then if pWeb then put "<hr>" & return after tOut else put "<p spacebelow=" & quote & "8" & quote & "><font color=" & quote & "#CBD5E1" & quote & ">" & mdRepeat("—", 16) & "</font></p>" after tOut end if else put mdInline(word 1 to -1 of tLine, pWeb) into tRest if pWeb then put "<p>" & tRest & "</p>" & return after tOut else put "<p spacebelow=" & quote & "8" & quote & ">" & tRest & "</p>" after tOut end if end if end repeat -- a block of code or a list still open at the end if tInCode then if pWeb then put "<pre><code>" & tCode & "</code></pre>" & return after tOut else put "<p bgcolor=" & quote & "#F1F5F9" & quote & " padding=" & quote & "8" & quote & "><font color=" & quote & "#0F766E" & quote & ">" & tCode & "</font></p>" after tOut end if end if if tList is not empty and pWeb then put "</" & tList & ">" & return after tOut return tOut end mdHtml function mdRepeat pText, pCount local tOut, k repeat with k = 1 to pCount put pText after tOut end repeat return tOut end mdRepeat -- Is the line an item of a list: "- ", "* ", "+ " or "1. "? function mdIsItem pLine local tWord put word 1 of pLine into tWord if tWord is "-" or tWord is "*" or tWord is "+" then return word 2 of pLine is not empty if char -1 of tWord is "." and char 1 to -2 of tWord is an integer then return word 2 of pLine is not empty return false end mdIsItem -- An item of a list. Line 1 of the result is the kind of list it is in -- (ul or ol), the rest is its HTML. function mdItem pLine, pWeb, pList local tWord, tKind, tRest, tOut, tBox put word 1 of pLine into tWord put word 2 to -1 of pLine into tRest if tWord is "-" or tWord is "*" or tWord is "+" then put "ul" into tKind else put "ol" into tKind end if -- a thing to do: "- [ ] ..." or "- [x] ..." put empty into tBox if tKind is "ul" and (char 1 to 3 of tRest is "[ ]" or char 1 to 3 of tRest is "[x]" or char 1 to 3 of tRest is "[X]") then put char 2 of tRest is not space into tBox delete char 1 to 3 of tRest put word 1 to -1 of tRest into tRest end if put mdInline(tRest, pWeb) into tRest if pWeb then if pList is not tKind then if pList is not empty then put "</" & pList & ">" & return after tOut put "<" & tKind & ">" & return after tOut end if if tBox is empty then put "<li>" & tRest & "</li>" & return after tOut else if tBox then put "<li class=" & quote & "task" & quote & "><input type=" & quote & "checkbox" & quote & " checked disabled> <s>" & tRest & "</s></li>" & return after tOut else put "<li class=" & quote & "task" & quote & "><input type=" & quote & "checkbox" & quote & " disabled> " & tRest & "</li>" & return after tOut end if else if tBox is empty then if tKind is "ul" then put "<p leftindent=" & quote & "22" & quote & " firstindent=" & quote & "-14" & quote & " spacebelow=" & quote & "5" & quote & "><font color=" & quote & kAccent & quote & "><b>•</b></font> " & tRest & "</p>" after tOut else put "<p leftindent=" & quote & "24" & quote & " firstindent=" & quote & "-20" & quote & " spacebelow=" & quote & "5" & quote & "><font color=" & quote & kAccent & quote & "><b>" & tWord & "</b></font> " & tRest & "</p>" after tOut end if else if tBox then put "<p leftindent=" & quote & "26" & quote & " firstindent=" & quote & "-24" & quote & " spacebelow=" & quote & "5" & quote & "><font color=" & quote & "#FFFFFF" & quote & " bgcolor=" & quote & "#16A34A" & quote & "><b> √ </b></font>  <font color=" & quote & "#94A3B8" & quote & "><strike>" & tRest & "</strike></font></p>" after tOut else put "<p leftindent=" & quote & "26" & quote & " firstindent=" & quote & "-24" & quote & " spacebelow=" & quote & "5" & quote & "><font color=" & quote & "#E2E8F0" & quote & " bgcolor=" & quote & "#E2E8F0" & quote & "><b> √ </b></font>  " & tRest & "</p>" after tOut end if return tKind & return & tOut end mdItem function mdEscape pText replace "&" with "&" in pText replace "<" with "<" in pText replace ">" with ">" in pText return pText end mdEscape -- Inside a line: `code`, **bold**, ~~struck~~, *italic* and [links](...) function mdInline pText, pWeb local tOut, tA, tB, tCode put mdEscape(pText) into pText -- code first, with its own stars and brackets hidden from the rest repeat put offset("`", pText) into tA if tA is 0 then exit repeat put offset("`", pText, tA) into tB if tB is 0 then exit repeat add tA to tB put char tA + 1 to tB - 1 of pText into tCode replace "*" with "*" in tCode replace "~" with "~" in tCode replace "[" with "[" in tCode replace "]" with "]" in tCode if pWeb then put char 1 to tA - 1 of pText & "<code>" & tCode & "</code>" after tOut else put char 1 to tA - 1 of pText & "<font color=" & quote & "#BE185D" & quote & " bgcolor=" & quote & "#F1F5F9" & quote & "> " & tCode & " </font>" after tOut end if delete char 1 to tB of pText end repeat put pText after tOut if pWeb then put mdPairs(tOut, "**", "<strong>", "</strong>") into tOut put mdPairs(tOut, "~~", "<del>", "</del>") into tOut put mdPairs(tOut, "*", "<em>", "</em>") into tOut else put mdPairs(tOut, "**", "<b>", "</b>") into tOut put mdPairs(tOut, "~~", "<strike>", "</strike>") into tOut put mdPairs(tOut, "*", "<i><font color=" & quote & "#7C3AED" & quote & ">", "</font></i>") into tOut end if return mdLinks(tOut) end mdInline -- Every pair of pMark around some text becomes pOpen and pClose function mdPairs pText, pMark, pOpen, pClose local tOut, tA, tB, tLen put length(pMark) into tLen repeat put offset(pMark, pText) into tA if tA is 0 then exit repeat put offset(pMark, pText, tA + tLen - 1) into tB if tB is 0 then exit repeat add tA + tLen - 1 to tB if tB is tA + tLen then -- nothing between them: keep them as they are put char 1 to tB + tLen - 1 of pText after tOut else put char 1 to tA - 1 of pText & pOpen & char tA + tLen to tB - 1 of pText & pClose after tOut end if delete char 1 to tB + tLen - 1 of pText end repeat return tOut & pText end mdPairs -- [words](address) function mdLinks pText local tOut, tA, tB, tC, tUrl repeat put offset("[", pText) into tA if tA is 0 then exit repeat put offset("](", pText, tA) into tB if tB is 0 then exit repeat add tA to tB put offset(")", pText, tB) into tC if tC is 0 then exit repeat add tB to tC put char tB + 2 to tC - 1 of pText into tUrl replace quote with "%22" in tUrl put char 1 to tA - 1 of pText & "<a href=" & quote & tUrl & quote & ">" & char tA + 1 to tB - 1 of pText & "</a>" after tOut delete char 1 to tC of pText end repeat return tOut & pText end mdLinks -- ----------------------------------------------------------------------- -- The buttons -- ----------------------------------------------------------------------- -- Puts Markdown around the selection, or at the start of its line command mdTool pName local tFrom, tTo, tSel, tOpen, tClose, tLine, tText, tWas if the selectedField is empty or the short name of the selectedField is not "Editor" then focus on field "Editor" select after text of field "Editor" end if put word 2 of the selectedChunk into tFrom put word 4 of the selectedChunk into tTo if pName is among the items of "H,List,Task" then put word 2 of the selectedLine into tLine if tLine is empty then put 1 into tLine put line tLine of field "Editor" into tText put tText into tWas switch pName case "H" if char 1 to 4 of tText is "### " then delete char 1 to 4 of tText else if char 1 to 3 of tText is "## " then put "#" before tText else if char 1 to 2 of tText is "# " then put "#" before tText else put "# " before tText end if break case "List" if char 1 to 2 of tText is "- " and char 3 to 5 of tText is not "[ ]" then delete char 1 to 2 of tText else put "- " before tText end if break case "Task" if char 1 to 6 of tText is "- [ ] " or char 1 to 6 of tText is "- [x] " then delete char 1 to 6 of tText else put "- [ ] " before tText end if break end switch put tText into line tLine of field "Editor" select after line tLine of field "Editor" mdChanged exit mdTool end if put char tFrom to tTo of field "Editor" into tSel switch pName case "B" put "**" into tOpen put "**" into tClose if tSel is empty then put "bold words" into tSel break case "I" put "*" into tOpen put "*" into tClose if tSel is empty then put "slanted words" into tSel break case "Code" put "`" into tOpen put "`" into tClose if tSel is empty then put "code" into tSel break case "Link" put "[" into tOpen put "](https://example.com)" into tClose if tSel is empty then put "a link" into tSel break end switch if tTo < tFrom then if tFrom > the number of chars of field "Editor" then put tOpen & tSel & tClose after field "Editor" else put tOpen & tSel & tClose before char tFrom of field "Editor" end if else put tOpen & tSel & tClose into char tFrom to tTo of field "Editor" end if select char tFrom + length(tOpen) to tFrom + length(tOpen) + length(tSel) - 1 of field "Editor" mdChanged end mdTool command mdNew local tId if the number of items of sIds >= kMax then mdToast "This sample keeps eight notes. Delete one to start another." exit mdNew end if mdAdd "# New note" & return & return, the seconds put the result into tId mdOpen tId focus on field "Editor" select char 3 to 10 of field "Editor" mdSaveSoon end mdNew command mdDelete local tTitle put mdTitle(sNotes[sCur]["text"]) into tTitle answer "Delete " & quote & tTitle & quote & "?" with "Cancel" or "Delete" if it is not "Delete" then exit mdDelete mdDrop sCur delete variable sNotes[sCur] if sIds is empty then mdAdd "# New note" & return & return, the seconds mdOpen item 1 of sIds mdSave mdToast "Deleted " & quote & tTitle & quote & "." end mdDelete -- Downloads the note as Markdown or as a web page command mdDownload pKind local tTitle, tName, tChar, tPath, tData put mdTitle(sNotes[sCur]["text"]) into tTitle repeat for each char tChar in tTitle if tChar is a number or (tChar >= "a" and tChar <= "z") or (tChar >= "A" and tChar <= "Z") or tChar is "-" then put tChar after tName else if tChar is space then put "-" after tName end if end repeat if tName is empty then put "note" into tName ask file "Save the note as:" with tName & "." & pKind if it is empty then exit mdDownload put it into tPath if pKind is "md" then put sNotes[sCur]["text"] into tData else put "<!doctype html>" & return & "<html><head><meta charset=" & quote & "utf-8" & quote & ">" & return & \ "<meta name=" & quote & "viewport" & quote & " content=" & quote & "width=device-width, initial-scale=1" & quote & ">" & return & \ "<title>" & mdEscape(tTitle) & "</title>" & return & "<style>" & return & \ "body { font: 16px/1.6 system-ui, sans-serif; max-width: 42rem; margin: 3rem auto; padding: 0 1rem; color: #1e293b; }" & return & \ "h1, h2, h3 { color: #0f172a; line-height: 1.25; } a { color: #2563eb; }" & return & \ "code { background: #f1f5f9; color: #be185d; padding: .1em .35em; border-radius: 4px; }" & return & \ "pre { background: #f1f5f9; padding: 1rem; border-radius: 8px; overflow: auto; } pre code { color: #0f766e; padding: 0; }" & return & \ "blockquote { margin: 1rem 0; padding: .6rem 1rem; background: #eff6ff; border-left: 4px solid #93c5fd; color: #475569; font-style: italic; }" & return & \ "li.task { list-style: none; margin-left: -1.4rem; } li.task s { color: #94a3b8; }" & return & \ "</style></head><body>" & return & mdHtml(sNotes[sCur]["text"], true) & "</body></html>" & return into tData end if put textEncode(tData, "UTF-8") into url ("binfile:" & tPath) if the result is not empty then mdToast "Could not save: " & the result else if the platform is "HTML5" then set the itemDelimiter to "/" mdToast "Downloaded " & the last item of tPath & "." set the itemDelimiter to comma else mdToast "Saved " & tPath & "." end if end mdDownload -- A short message at the bottom that goes away by itself command mdToast pText local tW set the text of field "ToastText" to pText put the formattedWidth of field "ToastText" + 40 into tW set the rect of graphic "ToastBack" to 450 - tW div 2, 520, 450 + tW div 2, 552 -- the text starts at the top of its field: 531 puts it in the middle set the rect of field "ToastText" to 450 - tW div 2, 531, 450 + tW div 2, 552 set the visible of group "Toast" to true if sToastTimer is not empty then cancel sToastTimer send "mdToastHide" to me in 2600 milliseconds put the result into sToastTimer end mdToast command mdToastHide put empty into sToastTimer set the visible of group "Toast" to false end mdToastHide -- ----------------------------------------------------------------------- -- Scrolling: the mouse wheel, and a thin bar of our own beside each field -- ----------------------------------------------------------------------- on rawKeyDown pKey local tField if pKey is not 65308 and pKey is not 65309 then pass rawKeyDown if the mouseH < the left of field "Preview" then put "Editor" into tField else put "Preview" into tField end if if pKey is 65308 then set the vScroll of field tField to the vScroll of field tField + 48 else set the vScroll of field tField to max(0, the vScroll of field tField - 48) end if mdThumbs end rawKeyDown on selectionChanged mdThumbs pass selectionChanged end selectionChanged command mdThumbs mdThumb "Editor", "EditThumb" mdThumb "Preview", "ViewThumb" end mdThumbs command mdThumb pField, pThumb local tTotal, tSeen, tTrack, tH, tTop, tX put the formattedHeight of field pField into tTotal put the height of field pField into tSeen if tTotal <= tSeen + 2 then set the visible of graphic pThumb to false exit mdThumb end if put tSeen - 12 into tTrack put max(28, round(tTrack * tSeen / tTotal)) into tH put the top of field pField + 6 + round((tTrack - tH) * the vScroll of field pField / (tTotal - tSeen)) into tTop put the right of field pField - 2 into tX set the rect of graphic pThumb to tX - 6, tTop, tX, tTop + tH set the visible of graphic pThumb to true end mdThumb -- Dragging a bar scrolls its field on mouseDown pButton local tName, tField, tTotal, tSeen, tTrack, tStart, tScroll, tY, tH put the short name of the target into tName if tName is not "EditThumb" and tName is not "ViewThumb" then pass mouseDown if tName is "EditThumb" then put "Editor" into tField else put "Preview" into tField end if put the formattedHeight of field tField into tTotal put the height of field tField into tSeen put tSeen - 12 - the height of graphic tName into tTrack put the mouseV into tStart put the vScroll of field tField into tScroll repeat while the mouse is down put the mouseV into tY if tTrack > 0 then set the vScroll of field tField to max(0, tScroll + round((tY - tStart) * (tTotal - tSeen) / tTrack)) end if mdThumb tField, tName wait 16 milliseconds with messages end repeat end mouseDown
button "View script" (card 1)
Markdown Notes.oxtstack, stack "Markdown Notes", 9 lines
-- Opens the Script Viewer: the scripts of this stack, in colour. on mouseUp if the mode of stack "Markdown Notes Script Viewer" is not 0 then close stack "Markdown Notes Script Viewer" else dispatch "svOpen" to card "Viewer" of stack "Markdown Notes Script Viewer" with the short name of this stack end if end mouseUp
button "New note" (card 1)
Markdown Notes.oxtstack, stack "Markdown Notes", 5 lines
-- Starts a new note. on mouseUp mdNew end mouseUp
graphic "rowBack1" (card 1)
Markdown Notes.oxtstack, stack "Markdown Notes", 2 lines
-- Opens this note: the card script does it.
graphic "rowBack2" (card 1)
Markdown Notes.oxtstack, stack "Markdown Notes", 2 lines
-- Opens this note: the card script does it.
graphic "rowBack3" (card 1)
Markdown Notes.oxtstack, stack "Markdown Notes", 2 lines
-- Opens this note: the card script does it.
graphic "rowBack4" (card 1)
Markdown Notes.oxtstack, stack "Markdown Notes", 2 lines
-- Opens this note: the card script does it.
graphic "rowBack5" (card 1)
Markdown Notes.oxtstack, stack "Markdown Notes", 2 lines
-- Opens this note: the card script does it.
graphic "rowBack6" (card 1)
Markdown Notes.oxtstack, stack "Markdown Notes", 2 lines
-- Opens this note: the card script does it.
graphic "rowBack7" (card 1)
Markdown Notes.oxtstack, stack "Markdown Notes", 2 lines
-- Opens this note: the card script does it.
graphic "rowBack8" (card 1)
Markdown Notes.oxtstack, stack "Markdown Notes", 2 lines
-- Opens this note: the card script does it.
button "Tool H" (card 1)
Markdown Notes.oxtstack, stack "Markdown Notes", 5 lines
-- Puts Markdown around the selection. on mouseUp mdTool "H" end mouseUp
button "Tool B" (card 1)
Markdown Notes.oxtstack, stack "Markdown Notes", 5 lines
-- Puts Markdown around the selection. on mouseUp mdTool "B" end mouseUp
button "Tool I" (card 1)
Markdown Notes.oxtstack, stack "Markdown Notes", 5 lines
-- Puts Markdown around the selection. on mouseUp mdTool "I" end mouseUp
button "Tool List" (card 1)
Markdown Notes.oxtstack, stack "Markdown Notes", 5 lines
-- Puts Markdown around the selection. on mouseUp mdTool "List" end mouseUp
button "Tool Task" (card 1)
Markdown Notes.oxtstack, stack "Markdown Notes", 5 lines
-- Puts Markdown around the selection. on mouseUp mdTool "Task" end mouseUp
button "Tool Code" (card 1)
Markdown Notes.oxtstack, stack "Markdown Notes", 5 lines
-- Puts Markdown around the selection. on mouseUp mdTool "Code" end mouseUp
button "Tool Link" (card 1)
Markdown Notes.oxtstack, stack "Markdown Notes", 5 lines
-- Puts Markdown around the selection. on mouseUp mdTool "Link" end mouseUp
button "Delete" (card 1)
Markdown Notes.oxtstack, stack "Markdown Notes", 5 lines
-- Deletes this note. on mouseUp mdDelete end mouseUp
field "Editor" (card 1)
Markdown Notes.oxtstack, stack "Markdown Notes", 5 lines
-- Every change is shown on the right, and kept. on textChanged mdChanged end textChanged
button "Get md" (card 1)
Markdown Notes.oxtstack, stack "Markdown Notes", 5 lines
-- Downloads the note as Markdown. on mouseUp mdDownload "md" end mouseUp
button "Get html" (card 1)
Markdown Notes.oxtstack, stack "Markdown Notes", 5 lines
-- Downloads the note as a web page. on mouseUp mdDownload "html" end mouseUp
field "Preview" (card 1)
Markdown Notes.oxtstack, stack "Markdown Notes", 2 lines
-- Ticks a box, or opens a link: the card script does both.
graphic "EditThumb" (card 1)
Markdown Notes.oxtstack, stack "Markdown Notes", 2 lines
-- Drag it to scroll: the card script does it.
graphic "ViewThumb" (card 1)
Markdown Notes.oxtstack, stack "Markdown Notes", 2 lines
-- Drag it to scroll: the card script does it.
the stack script
Markdown Notes.oxtstack, stack "Markdown Notes Script Viewer", 28 lines
-- The Script Viewer's own messages stop here, so they never reach the -- stack it belongs to: that stack's openCard, mouseUp and arrowKey -- handlers are for its own cards. on preOpenStack end preOpenStack on openStack end openStack on preOpenCard end preOpenCard on openCard end openCard on closeCard end closeCard on closeStack end closeStack on mouseUp end mouseUp on mouseDown end mouseDown on arrowKey pKey -- up and down move through the list if pKey is "up" then svMove -1 if pKey is "down" then svMove 1 end arrowKey on escapeKey close this stack end escapeKey
card "Viewer" (card 1)
Markdown Notes.oxtstack, stack "Markdown Notes Script Viewer", 369 lines
-- ===== The Script Viewer ===== -- Lists the scripts of the card in front of the stack that opened it, -- and shows the one chosen in colour: comments, strings, numbers, -- keywords, commands and the names of handlers each in their own -- colour, with line numbers. Any stack opens it with svOpen: -- -- dispatch "svOpen" to card 1 of stack "Markdown Notes Script Viewer" with "My Stack" -- -- Controls are named "of me" (this card), so these handlers work even -- while another stack is the default stack. -- The colours of the code, on its dark background. constant kInk = "#E0DEF4" constant kComment = "#7F8FA6" constant kString = "#F6C177" constant kNumber = "#F8A978" constant kKeyword = "#C4A7E7" constant kCommand = "#9CCFD8" constant kDeclare = "#EB6F92" constant kName = "#82AAFF" constant kSoft = "#A6A3C4" -- Words of each kind. A word may be in only one list. constant kKeywords = "if,then,else,end,repeat,for,each,while,until,forever,times,switch,case,default,break,exit,next,pass,return,try,catch,finally,throw,do,send,dispatch,call,with,in" constant kCommands = "put,set,get,go,answer,ask,create,delete,show,hide,lock,unlock,wait,add,subtract,multiply,divide,sort,replace,open,close,palette,move,play,beep,select,copy,paste,start,stop,save,write,read,launch,click,type,focus,choose,cancel,import,export,print,filter,split,combine,load,post" constant kSoftWords = "the,of,into,after,before,and,or,not,is,a,an,to,me,it,empty,true,false,quote,space,tab,comma,cr,return,line,lines,word,words,char,chars,item,items,field,button,card,stack,graphic,group,this,target,among,contains,there" constant kDeclares = "on,command,function,private,getprop,setprop,local,global,constant" local sTarget -- the name of the stack whose scripts are listed local sIDs -- the long id of the object on each line of the list local sInBlock -- true inside a /* ... */ comment while colouring -- Open the viewer for a stack, beside its window, listing the scripts -- of its card in front. pChoose "stack" chooses the stack script. command svOpen pStack, pChoose local tR, tScreen, tHeight, tLeft, tTop put pStack into sTarget put the rect of stack pStack into tR put the working screenRect into tScreen -- As tall as the screen allows, keeping room above for the title bar, -- so that the window can always be dragged. put min(640, item 4 of tScreen - item 2 of tScreen - 44) into tHeight put max(440, tHeight) into tHeight if the height of this stack is not tHeight then set the height of this stack to tHeight end if svLayout -- To the right of the stack's window, or to its left, or over it. put item 3 of tR + 12 into tLeft if tLeft + the width of this stack > item 3 of tScreen then put item 1 of tR - 12 - the width of this stack into tLeft end if if tLeft < item 1 of tScreen then put item 3 of tR - the width of this stack - 12 into tLeft end if put max(item 1 of tScreen, tLeft) into tLeft -- Level with the stack, but never above the screen or off its bottom. put max(item 2 of tR, item 2 of tScreen + 32) into tTop if tTop + tHeight > item 4 of tScreen - 8 then put item 4 of tScreen - 8 - tHeight into tTop end if put max(tTop, item 2 of tScreen + 32) into tTop set the topLeft of this stack to tLeft, tTop -- Open first: a list field keeps its chosen line only while open. if the mode of this stack is 0 then palette this stack end if svRefresh pChoose end svOpen -- The window can be made shorter or taller: the list and the code share -- the height, and the buttons stay at the bottom. on resizeStack svLayout end resizeStack command svLayout local tH, tList, tY, tButton put the height of this stack into tH put max(70, 140 - max(0, 600 - tH)) into tList lock screen set the rect of field "svObjects" of me to 16, 78, 524, 78 + tList put 78 + tList + 4 into tY set the rect of button "svOnly" of me to 16, tY, 330, tY + 22 add 28 to tY set the rect of field "svAbout" of me to 16, tY, 524, tY + 54 add 62 to tY set the rect of field "svGutter" of me to 16, tY, 58, tH - 52 set the rect of field "svCode" of me to 58, tY, 524, tH - 52 repeat for each item tButton in "svCopy,svShowMe,svEdit,svClose" set the bottom of button tButton of me to tH - 12 end repeat unlock screen end svLayout -- Rebuild the list for the target's card in front. command svRefresh pChoose local tCard, tMain, tList, tObj, i, tOnly, tPrevious, tLine if sTarget is empty or there is not a stack sTarget then exit svRefresh -- Remember which object was chosen, to choose it again. put line (the hilitedLine of field "svObjects" of me) of sIDs into tPrevious put the long id of stack sTarget into tMain put the long id of this card of stack sTarget into tCard put sTarget & ": the card" && quote & the short name of tCard & quote into field "svSub" of me put the hilite of button "svOnly" of me into tOnly -- The stack and the card come first: they are always listed. put "Stack script" & tab & svCount(tMain) into tList put tMain into sIDs put return & "Card script" & tab & svCount(tCard) after tList put return & tCard after sIDs -- Then each control of the card, from the back to the front. repeat with i = 1 to the number of controls of tCard put the long id of control i of tCard into tObj if tOnly and the script of tObj is empty then next repeat put return after tList -- Controls in a group are indented under it. if word 1 of the owner of tObj is "group" then put " " after tList put the name of tObj & tab & svCount(tObj) after tList put return & tObj after sIDs end repeat put tList into field "svObjects" of me -- Choose as asked, or the same object again, or else the card script. put 2 into tLine if pChoose is "stack" then put 1 into tLine else if tPrevious is not empty then set the wholeMatches to true if lineOffset(tPrevious, sIDs) > 0 then put lineOffset(tPrevious, sIDs) into tLine end if set the hilitedLine of field "svObjects" of me to tLine svShow end svRefresh -- "12 lines", or "no script". function svCount pObj local tCount put the number of lines of the script of pObj into tCount if tCount is 0 then return "no script" if tCount is 1 then return "1 line" return tCount && "lines" end svCount -- The long id of the object chosen in the list (empty when none). function svCurrent local tLine put the hilitedLine of field "svObjects" of me into tLine if tLine is empty then return empty return line tLine of sIDs end svCurrent -- Show the chosen object's script, and a word on what it is for. command svShow local tObj, tScript, tNumbers, i put svCurrent() into tObj if tObj is empty then exit svShow if not exists(tObj) then -- It was deleted since the list was made. svRefresh exit svShow end if lock screen put svAbout(tObj) into field "svAbout" of me put the script of tObj into tScript if tScript is empty then put "-- This object has no script." into tScript -- htmlText sets the text and its colours in one go. set the htmlText of field "svCode" of me to svColoured(tScript) repeat with i = 1 to the number of lines of tScript put i & return after tNumbers end repeat put char 1 to -2 of tNumbers into field "svGutter" of me set the vScroll of field "svCode" of me to 0 set the hScroll of field "svCode" of me to 0 set the vScroll of field "svGutter" of me to 0 unlock screen end svShow -- The script as htmlText, one coloured paragraph per line. function svColoured pScript local tLine, tHtml put false into sInBlock repeat for each line tLine in pScript put "<p>" & svColourLine(tLine) & "</p>" after tHtml end repeat return tHtml end svColoured -- One line, coloured word by word. Walks the line a character at a -- time: a quote starts a string, -- or # a comment to the end of the -- line, /* a comment to */, a letter a word. function svColourLine pLine local tOut, tLen, i, tChar, tStart, tWord, tPrev, tEnd put the number of chars of pLine into tLen put 1 into i if sInBlock then put offset("*/", pLine) into tEnd if tEnd is 0 then return svSpan(pLine, kComment) put svSpan(char 1 to tEnd + 1 of pLine, kComment) into tOut put false into sInBlock put tEnd + 2 into i end if repeat while i <= tLen put char i of pLine into tChar if tChar is quote then put i into tStart add 1 to i repeat while i <= tLen and char i of pLine is not quote add 1 to i end repeat put svSpan(char tStart to i of pLine, kString) after tOut add 1 to i else if char i to i + 1 of pLine is "--" or tChar is "#" then put svSpan(char i to tLen of pLine, kComment) after tOut exit repeat else if char i to i + 1 of pLine is "/*" then put offset("*/", char i + 2 to tLen of pLine) into tEnd if tEnd is 0 then put svSpan(char i to tLen of pLine, kComment) after tOut put true into sInBlock exit repeat end if put svSpan(char i to i + tEnd + 2 of pLine, kComment) after tOut add tEnd + 3 to i else if svIsWordChar(tChar) then put i into tStart repeat while i <= tLen and svIsWordChar(char i of pLine) add 1 to i end repeat put char tStart to i - 1 of pLine into tWord put svSpan(tWord, svWordColour(tWord, tPrev)) after tOut put tWord into tPrev else put svEscape(tChar) after tOut add 1 to i end if end repeat return tOut end svColourLine -- The colour of a word, which can depend on the word before it: the -- word after on, command or function is the name of a handler. function svWordColour pWord, pPrevious if pPrevious is among the items of "on,command,function,getProp,setProp" then return kName if pWord is a number then return kNumber if pWord is among the items of kDeclares then return kDeclare if pWord is among the items of kKeywords then return kKeyword if pWord is among the items of kCommands then return kCommand if pWord is among the items of kSoftWords then return kSoft return kInk end svWordColour function svIsWordChar pChar return pChar is in "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_" end svIsWordChar function svSpan pText, pColour return "<font color=" & quote & pColour & quote & ">" & svEscape(pText) & "</font>" end svSpan -- < > and & mean something in HTML: write them as entities. function svEscape pText replace "&" with "&" in pText replace "<" with "<" in pText replace ">" with ">" in pText return pText end svEscape -- A sentence or two on what this kind of script does, and its handlers. function svAbout pObj local tKind, tText, tHandlers put word 1 of the name of pObj into tKind switch tKind case "stack" put "The stack script is the last stop on the message path for every card, so its handlers can be called from anywhere in the stack." into tText break case "card" put "A card script gets the card's own messages (openCard, closeCard) and the messages of its controls that they do not handle." into tText break case "group" put "A group script gets the messages of the controls inside the group that they do not handle themselves." into tText break default if the script of pObj is not empty then put "This" && tKind & "'s handlers run when it gets the messages they are named after: mouseUp for a click." into tText else if the cGoTo of pObj is not empty then put "No script: its click goes up the message path to the stack script, which reads its cGoTo property (" & quote & the cGoTo of pObj & quote & ")." into tText else if word 1 of the owner of pObj is "group" then put "No script: its messages go on to its group," && the short name of the owner of pObj & "." into tText else put "No script: this" && tKind && "only shows something, and other scripts change it." into tText end if end switch put svHandlers(the script of pObj) into tHandlers if tHandlers is not empty then put return & "Handlers: " & tHandlers after tText return tText end svAbout -- The names of the handlers in a script, joined with commas. function svHandlers pScript local tLine, tDecl, tList repeat for each line tLine in pScript put tLine into tDecl if word 1 of tDecl is "private" then delete word 1 of tDecl if word 1 of tDecl is among the items of "on,command,function,getProp,setProp" then put word 2 of tDecl & ", " after tList end if end repeat if tList is not empty then delete char -2 to -1 of tList return tList end svHandlers -- Copy the chosen script, and say so on the button for a moment. command svCopy local tObj put svCurrent() into tObj if tObj is empty then exit svCopy set the clipboardData["text"] to the script of tObj set the label of button "svCopy" of me to "Copied!" send "svCopyDone" to me in 1200 milliseconds end svCopy on svCopyDone set the label of button "svCopy" of me to "Copy" end svCopyDone -- Open the chosen script in the Script Editor (only in the IDE). command svEdit local tObj put svCurrent() into tObj if tObj is empty then exit svEdit edit the script of tObj end svEdit -- Make the chosen control blink, to show where it is. command svShowMe local tObj put svCurrent() into tObj if tObj is empty then exit svShowMe if word 1 of the name of tObj is among the items of "stack,card" then put "Show me works on a control: choose one in the list." into field "svAbout" of me exit svShowMe end if if not the visible of tObj then put "This control is hidden right now, so it cannot blink." into field "svAbout" of me exit svShowMe end if repeat 3 times set the visible of tObj to false wait 150 milliseconds with messages set the visible of tObj to true wait 150 milliseconds with messages end repeat end svShowMe -- The arrow keys move the choice in the list. command svMove pStep local tLine put the hilitedLine of field "svObjects" of me into tLine if tLine is empty then put 0 into tLine put max(1, min(the number of lines of field "svObjects" of me, tLine + pStep)) into tLine set the hilitedLine of field "svObjects" of me to tLine svShow end svMove -- Edit script is shown only in the IDE, where the Script Editor is. on preOpenCard set the visible of button "svEdit" of me to (the environment is "development") end preOpenCard
field "svObjects" (card 1)
Markdown Notes.oxtstack, stack "Markdown Notes Script Viewer", 5 lines
-- Show the script of the object clicked. svShow is in the card script. on mouseUp svShow end mouseUp
button "svOnly" (card 1)
Markdown Notes.oxtstack, stack "Markdown Notes Script Viewer", 5 lines
-- List every control, or only those with a script. on mouseUp svRefresh end mouseUp
field "svCode" (card 1)
Markdown Notes.oxtstack, stack "Markdown Notes Script Viewer", 5 lines
-- The line numbers scroll with the code. on scrollbarDrag set the vScroll of field "svGutter" of me to the vScroll of me end scrollbarDrag
button "svCopy" (card 1)
Markdown Notes.oxtstack, stack "Markdown Notes Script Viewer", 4 lines
on mouseUp svCopy end mouseUp
button "svShowMe" (card 1)
Markdown Notes.oxtstack, stack "Markdown Notes Script Viewer", 4 lines
on mouseUp svShowMe end mouseUp
button "svEdit" (card 1)
Markdown Notes.oxtstack, stack "Markdown Notes Script Viewer", 5 lines
-- edit opens the Script Editor, which only the IDE has. on mouseUp svEdit end mouseUp
button "svClose" (card 1)
Markdown Notes.oxtstack, stack "Markdown Notes Script Viewer", 4 lines
on mouseUp close this stack end mouseUp