From 8479010c18bfd594854e5661af00dda5cc758ebb Mon Sep 17 00:00:00 2001 From: johnnyq Date: Sun, 29 Nov 2026 22:55:08 -0500 Subject: [PATCH] Write portal documents in Markdown too: New Document in the client portal gets the Rich text and Markdown switch with the Markdown toolbar, Preview and help, converting either way as it is written and remembering the way used last, and its rich text editor shows the document as it will read with headings, quotes, code, tables and lines on its toolbar; both editors and the switch are now one shared include for agents and the portal, the portal converts Markdown at its own post.php for contacts who can write documents, Markdown documents from the portal open in Markdown for agents, and an empty document is refused in either mode; 26.12.3 changelog entries --- CHANGELOG.md | 2 + agent/modals/document/document_form.php | 94 +----------------- client/document_add.php | 23 +++-- client/includes/footer.php | 4 + client/post.php | 36 +++++-- includes/document_editors.php | 106 ++++++++++++++++++++ js/app.js | 43 +------- js/document_markdown.js | 10 +- js/portal.js | 124 ++++++++++++++++-------- js/tinymce_defaults.js | 41 ++++++++ 10 files changed, 296 insertions(+), 187 deletions(-) create mode 100644 includes/document_editors.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 2c11eedc2..1ff052d78 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -36,6 +36,7 @@ Update from Maintenance > Update as usual - this release has four database chang - Admin > Settings > Single Sign-on is gone: the client portal's Microsoft 365 sign-in is now a Microsoft 365 identity provider. A contact's Login of Only with an identity provider (once called With their Microsoft account) signs in only that way, with no password, and is offered when a provider is on for client contacts. - Printing a ticket prints a work order instead of the page as it looks on screen: your letterhead, the client and contact, the site with its access notes, the job's details, its assets, the description, the fill-ins the client can see, the tasks as boxes to tick, the public replies oldest first, and a sign-off with arrival and departure times, lines for the work done and signatures. Internal notes never print. It's Print Work Order in the ticket's menu, or the browser's own print. - Printing an asset prints a spec sheet: its hardware, network, where it is and who has it, its interfaces, the devices in it, its licenses and services and its quick notes, with a QR code that opens it in ITFlow. Credentials and license keys never print. The new print button sits beside Copy Details. +- Writing a document in the client portal works as it does for you: the editor shows it as it will read, with headings, lists, quotes, code, tables and lines on its toolbar, or contacts can write it in Markdown, with the Markdown toolbar and Preview, switching either way as they write. The next document starts in the way they wrote last. ### Bug Fixes @@ -71,6 +72,7 @@ Update from Maintenance > Update as usual - this release has four database chang - login_provider.php starts a sign-in (?provider=N, a single-use state in the session, ten minutes) and takes the code back, then hands the account to login.php as ?provider_login= with $_SESSION['pending_provider_login'], which signs in through its usual success path - role choice included - with $_SESSION['signed_in_with'] = 'provider' (left out of MFA enforcement as a passkey is) and signed_in_via the provider's name. client/login_microsoft.php serves it for the migrated app's redirect address. IMAP is checked in login.php's password step ('imap'); an administrator's own password still works there. - Admin: admin/identity_providers.php (list), admin/modals/identity_provider/identity_provider_edit.php (Add and Edit), admin/post/identity_provider.php (save_identity_provider, turn_identity_provider_on / off, delete_identity_provider - locked in the demo). The identity settings page and edit_identity_provider are gone. portalSignInMethods() and identityProviderContactsWording() take an email, to offer only the providers that sign it in; vaultLockedReason() says why the vault is locked. - functions/print_sheet.php draws the work order (ticketWorkOrderHtml()) and spec sheet (assetSpecSheetHtml()) into their pages, hidden on screen; css .itflow-print-sheet hides the rest of the page when printing (the .container-fluid:has() rule). [data-print-page] buttons call window.print() (js/app.js). The ticket's actions bar and menu now show for every agent. +- includes/document_editors.php is a document's two editors and the switch between them, shared by agent/modals/document/document_form.php and client/document_add.php (which now posts format, content and markdown, saved through documentPostedContent()). js/document_markdown.js converts at the address its script tag gives (data-markdown-url, client/post.php markdown_to_html and html_to_markdown in the portal). itflowProseContentStyle() moved to js/tinymce_defaults.js; js/portal.js sets up .tinymce-document, and its required check looks in the Markdown box when that is shown. ## [26.12.2] diff --git a/agent/modals/document/document_form.php b/agent/modals/document/document_form.php index d74db4d9e..dfdacb676 100644 --- a/agent/modals/document/document_form.php +++ b/agent/modals/document/document_form.php @@ -14,15 +14,10 @@ * agent used last. * * It is written in the rich text editor or in Markdown, picked above the editor - * and switched either way as it is written (js/document_markdown.js); the form - * posts format, with content or markdown. Both show it as it will read on its - * page (.itflow-prose): the rich text editor as it is written, Markdown in its - * Preview. + * and switched either way as it is written (includes/document_editors.php). Both + * show it as it will read on its page (.itflow-prose): the rich text editor as + * it is written, Markdown in its Preview. */ - -$document_markdown_ready = documentMarkdownReady(); -$document_is_markdown = $document_markdown_ready && isset($document_markdown) && $document_markdown !== null; -$document_format_uid = bin2hex(random_bytes(4)); ?>
@@ -70,88 +65,7 @@ $document_format_uid = bin2hex(random_bytes(4));
-
> - -
-
- > - - > - -
- -
- - -
> - - -
- - -
> - - - -
-
- - - - - - - -
# Heading ## SmallerHeadings**bold** _italic_ ~~struck~~Text
- item 1. itemLists - indent for a list inside one- [ ] to do - [x] doneChecklist
[words](https://...)Link`code`, or ``` on the lines above and belowCode
| A | B | then |---|---|Table> quote ---Quote, line
-
-
- - -
- -
- - - +
Saving keeps what it says now in the document's history, so it can be put back.
diff --git a/client/document_add.php b/client/document_add.php index 79f32b257..107452a4a 100644 --- a/client/document_add.php +++ b/client/document_add.php @@ -1,11 +1,14 @@ Documents @@ -66,8 +72,13 @@ $upload = !empty($_GET['upload']);
- - +
diff --git a/client/includes/footer.php b/client/includes/footer.php index 8e96f2160..d5d50ae93 100644 --- a/client/includes/footer.php +++ b/client/includes/footer.php @@ -28,6 +28,10 @@ + + + + diff --git a/client/post.php b/client/post.php index c5f0cccdd..65e72f12a 100644 --- a/client/post.php +++ b/client/post.php @@ -20,7 +20,7 @@ if (!empty($_SESSION['portal_view_as']) && (isset($_GET['exit_view_as']) || isse unset($_SESSION['portal_view_as']); redirect($back); } -if (portalViewingAs() && !isset($_GET['export_statement_pdf']) && !isset($_GET['kb_suggest'])) { +if (portalViewingAs() && !isset($_GET['export_statement_pdf']) && !isset($_GET['kb_suggest']) && !isset($_GET['markdown_to_html']) && !isset($_GET['html_to_markdown'])) { flashAlert("You're viewing the portal as " . escapeHtml($portal_view_as['contact_name']) . " - nothing can be changed", 'error'); redirect(); } @@ -28,6 +28,28 @@ if (portalViewingAs() && !isset($_GET['export_statement_pdf']) && !isset($_GET[' // Two-step sign-in required and not set up yet: client/includes/check_login.php has // already turned away everything but setting it up, or signing out +/* + * Markdown for New Document's editors (js/document_markdown.js, includes/document_editors.php) - + * Markdown as the HTML it shows as (Preview, switching to the rich text editor, Markdown pasted + * into it) and the rich text editor's HTML as Markdown, as agent/ajax.php does for agents. For + * whoever can write a document; it changes nothing. Answers {html}, or {markdown, kept, dropped}. + */ +if (isset($_GET['markdown_to_html']) || isset($_GET['html_to_markdown'])) { + header('Content-Type: application/json'); + if (!($config_module_enable_itdoc && contactCan('itdoc')) || !documentMarkdownReady()) { + exit(json_encode(['error' => 'Markdown isn\'t available here.'])); + } + $text = (string) ($_POST[isset($_GET['markdown_to_html']) ? 'markdown' : 'html'] ?? ''); + if (strlen($text) > 4 * 1024 * 1024) { + exit(json_encode(['error' => 'That is too long to convert - over 4 MB.'])); + } + if (isset($_GET['markdown_to_html'])) { + exit(json_encode(['html' => markdownToHtml($text)])); + } + $markdown = htmlToMarkdown($text, $markdown_notes); + exit(json_encode(['markdown' => $markdown, 'kept' => $markdown_notes['kept'], 'dropped' => $markdown_notes['dropped']])); +} + /* * The Help articles that might answer a new ticket's subject, as it is typed * on New ticket (js/portal.js) - at most three of the ones shared with their @@ -1748,10 +1770,12 @@ if (isset($_POST['client_add_document'])) { // Primary and technical contacts, with IT documentation on portalItGate(); - $raw_content = (string) ($_POST['document_content'] ?? ''); + // Written in the rich text editor, or in Markdown (includes/document_editors.php) + $is_markdown = documentMarkdownReady() && ($_POST['format'] ?? '') === 'markdown'; + $raw_content = $is_markdown ? markdownToHtml((string) ($_POST['markdown'] ?? '')) : (string) ($_POST['content'] ?? ''); $name = trim(mb_substr((string) ($_POST['document_name'] ?? ''), 0, 200)); $description = trim(mb_substr((string) ($_POST['document_description'] ?? ''), 0, 255)); - if ($name === '' || portalHtmlIsEmpty($raw_content)) { + if ($name === '' || ($is_markdown ? trim((string) ($_POST['markdown'] ?? '')) === '' : portalHtmlIsEmpty($raw_content))) { flashAlert('Give the document a name and write something in it', 'error'); redirect('document_add.php'); } @@ -1772,9 +1796,9 @@ if (isset($_POST['client_add_document'])) { $document_id = mysqli_insert_id($mysqli); - // Pasted pictures are saved as files rather than kept in the HTML - $processed_content = mysqli_escape_string($mysqli, saveBase64Images($raw_content, $_SERVER['DOCUMENT_ROOT'] . "/uploads/documents/", "uploads/documents/", $document_id)); - mysqli_query($mysqli, "UPDATE documents SET document_content = '$processed_content' WHERE document_id = $document_id"); + // Pasted pictures are saved as files rather than kept in the HTML - or in the Markdown + [$processed_html, $markdown] = documentPostedContent($document_id); + mysqli_query($mysqli, "UPDATE documents SET document_content = '" . mysqli_real_escape_string($mysqli, $processed_html) . "'" . documentMarkdownSet($markdown) . " WHERE document_id = $document_id"); logAudit("Document", "Create", "Client contact $session_contact_name created document $document_name in the client portal", $session_client_id, $document_id); appNotify("Document", "$session_contact_name added the document $document_name in the client portal", "/agent/document.php?document_id=$document_id", $session_client_id, $document_id); diff --git a/includes/document_editors.php b/includes/document_editors.php new file mode 100644 index 000000000..d4028223e --- /dev/null +++ b/includes/document_editors.php @@ -0,0 +1,106 @@ + +
> + +
+
+ > + + > + +
+ +
+ + +
> + + +
+ + +
> + + + +
+
+ + + + + + + +
# Heading ## SmallerHeadings**bold** _italic_ ~~struck~~Text
- item 1. itemLists - indent for a list inside one- [ ] to do - [x] doneChecklist
[words](https://...)Link`code`, or ``` on the lines above and belowCode
| A | B | then |---|---|Table> quote ---Quote, line
+
+
+ + +
+ + + + +
+ + + diff --git a/js/app.js b/js/app.js index f481fce04..518af0f16 100644 --- a/js/app.js +++ b/js/app.js @@ -428,46 +428,6 @@ function itflowTinyMceSkin() { }; } -/** - * A document as it reads on its page (.itflow-prose, css/itflow_custom.css), for the editing - * surface - which is an iframe, so the page's stylesheet and custom properties don't reach it - * and the values are passed in as literal text, as itflowTinyMceSkin() does. - */ -function itflowProseContentStyle() { - var root = getComputedStyle(document.documentElement); - - function v(name, fallback) { - var value = root.getPropertyValue(name).trim(); - return value || fallback; - } - - var border = v('--bs-border-color', '#dee2e6'); - var tint = v('--bs-tertiary-bg', '#f8f9fa'); - var muted = v('--bs-secondary-color', '#6c757d'); - var strong = v('--bs-emphasis-color', '#000'); - - return 'body{line-height:1.65;margin:1.25rem 1.5rem;}' + - 'body>:first-child{margin-top:0;}' + - 'h1,h2,h3,h4,h5,h6{margin:1.75rem 0 .6rem;font-weight:600;line-height:1.3;color:' + strong + ';}' + - 'h1{font-size:1.6rem;}' + - 'h2{font-size:1.3rem;padding-bottom:.35rem;border-bottom:1px solid ' + border + ';}' + - 'h3{font-size:1.12rem;}' + - 'h4{font-size:1rem;}' + - 'h5,h6{font-size:.9rem;text-transform:uppercase;letter-spacing:.03em;color:' + muted + ';}' + - 'p,ul,ol,table,pre,blockquote,figure{margin:0 0 1rem;}' + - 'ul,ol{padding-left:1.5rem;}' + - 'li+li{margin-top:.25rem;}' + - 'hr{margin:2rem 0;border:0;border-top:1px solid ' + border + ';}' + - 'img{max-width:100%;height:auto;border-radius:.375rem;}' + - 'table{border-collapse:collapse;font-size:.925rem;}' + - // Every table with its lines, as on the page - not TinyMCE's dashed outline for one without a border - 'table td,table th{padding:.4rem .65rem;border:1px solid ' + border + ' !important;}' + - 'table th,table thead td{background-color:' + tint + ';font-weight:600;}' + - 'pre{padding:.75rem 1rem;border:1px solid ' + border + ';border-radius:.375rem;background-color:' + tint + ';white-space:pre;overflow-x:auto;}' + - ':not(pre)>code{padding:.1rem .3rem;border-radius:.25rem;background-color:' + tint + ';color:' + strong + ';}' + - 'blockquote{margin-left:0;padding:.25rem 0 .25rem 1rem;border-left:.25rem solid ' + border + ';color:' + muted + ';}'; -} - /** * Chart.js defaults for the current colour mode. * @@ -677,7 +637,8 @@ function itflowInit() { // page, and with what a document is made of on the toolbar, as the Markdown box has const itflowDocumentWriter = { ...itflowDocumentEditor, - content_style: itflowDocumentEditor.content_style + itflowProseContentStyle(), + // js/tinymce_defaults.js - not on the few pages without the editor (Getting Started) + content_style: itflowDocumentEditor.content_style + (typeof itflowProseContentStyle === 'function' ? itflowProseContentStyle() : ''), block_formats: 'Normal text=p; Heading 1=h1; Heading 2=h2; Heading 3=h3; Heading 4=h4', toolbar: [ ...(itflowAiOn(false) ? [{ name: 'ai', items: ['reword'] }] : []), diff --git a/js/document_markdown.js b/js/document_markdown.js index bdde1b3b0..9b47c2543 100644 --- a/js/document_markdown.js +++ b/js/document_markdown.js @@ -20,7 +20,8 @@ * Markdown pasted into a document's rich text editor - copied from GitHub's source view, VS Code, * Obsidian, an AI chat - goes in formatted. Ctrl+Shift+V pastes it as it is. * - * Loaded on every agent page (includes/footer.php), once. + * Loaded on every agent page (includes/footer.php), once - and on New Document in the client + * portal (client/includes/footer.php), which turns Markdown into HTML at client/post.php. */ (function () { 'use strict'; @@ -31,8 +32,13 @@ var REMEMBER = 'itflow.documentFormat'; + // Where Markdown is turned into HTML and back - agent/ajax.php, or in the client portal the + // address its