import $ from "jquery";
import render_markdown_help from "../templates/markdown_help.hbs";
import * as browser_history from "./browser_history";
import * as common from "./common";
import * as components from "./components";
import * as keydown_util from "./keydown_util";
import * as markdown from "./markdown";
import * as overlays from "./overlays";
import * as popovers from "./popovers";
import * as rendered_markdown from "./rendered_markdown";
import * as ui from "./ui";
import * as util from "./util";
// Make it explicit that our toggler is undefined until
// set_up_toggler is called.
export let toggler;
const markdown_help_rows = [
{
markdown: "*italic*",
usage_html: "(or Ctrl+I)",
},
{
markdown: "**bold**",
usage_html: "(or Ctrl+B)",
},
{
markdown: "~~strikethrough~~",
},
{
markdown: "[Zulip website](https://zulip.org)",
usage_html: "(or Ctrl+Shift+L)",
},
{
markdown: `\
* Milk
* Tea
* Green tea
* Black tea
* Oolong tea
* Coffee`,
},
{
markdown: `\
1. Milk
1. Tea
1. Coffee`,
},
{
markdown: ":heart:",
usage_html:
'(and many others, from the Noto Project)',
},
{
markdown: "@**Joe Smith**",
usage_html: "(autocompletes from @joe)",
output_html: '
@Joe Smith
',
effect_html: "(notifies Joe Smith)",
},
{
markdown: "@_**Joe Smith**",
usage_html: "(autocompletes from @_joe)",
output_html: 'Joe Smith
',
effect_html: "(links to profile but doesn't notify Joe Smith)",
},
{
markdown: "@**all**",
effect_html: "(notifies all recipients)",
},
{
markdown: "#**streamName**",
output_html: "#streamName
",
effect_html: "(links to a stream)",
},
{
markdown: "/me is busy working",
output_html: 'Iago is busy working
',
usage_html: "(send a status message as user Iago)",
},
{
markdown: "Some inline `code`",
},
{
markdown: `\
\`\`\`
def zulip():
print "Zulip"
\`\`\``,
},
{
markdown: `\
\`\`\`python
def zulip():
print "Zulip"
\`\`\``,
output_html: `\
def zulip():
print "Zulip"
`,
},
{
note_html: i18n.t(
'To add syntax highlighting to a multi-line code block, add the language\'s first Pygments short name after the first set of back-ticks. You can also make a code block by indenting each line with 4 spaces.',
),
},
{
markdown: "> Quoted",
},
{
markdown: `\
\`\`\`quote
Quoted block
\`\`\``,
},
{
markdown: `\
\`\`\`spoiler Always visible heading
This text won't be visible until the user clicks.
\`\`\``,
},
{
markdown: "Some inline math $$ e^{i \\pi} + 1 = 0 $$",
},
{
markdown: `\
\`\`\`math
\\int_{0}^{1} f(x) dx
\`\`\``,
},
{
note_html: i18n.t(
'You can also make tables with this Markdown-ish table syntax.',
),
},
];
export function set_up_toggler() {
for (const row of markdown_help_rows) {
if (row.markdown && !row.output_html) {
const message = {raw_content: row.markdown};
markdown.apply_markdown(message);
row.output_html = util.clean_user_content_links(message.content);
}
}
const $markdown_help = $(render_markdown_help({markdown_help_rows}));
$markdown_help.find(".rendered_markdown").each(function () {
rendered_markdown.update_elements($(this));
});
$(".informational-overlays .overlay-body").append($markdown_help);
const opts = {
selected: 0,
child_wants_focus: true,
values: [
{label: i18n.t("Keyboard shortcuts"), key: "keyboard-shortcuts"},
{label: i18n.t("Message formatting"), key: "message-formatting"},
{label: i18n.t("Search operators"), key: "search-operators"},
],
callback(name, key) {
$(".overlay-modal").hide();
$(`#${CSS.escape(key)}`).show();
ui.get_scroll_element($(`#${CSS.escape(key)}`).find(".modal-body")).trigger("focus");
},
};
toggler = components.toggle(opts);
const elem = toggler.get();
elem.addClass("large allow-overflow");
const modals = opts.values.map((item) => {
const key = item.key; // e.g. message-formatting
const modal = $(`#${CSS.escape(key)}`).find(".modal-body");
return modal;
});
for (const modal of modals) {
ui.get_scroll_element(modal).prop("tabindex", 0);
keydown_util.handle({
elem: modal,
handlers: {
left_arrow: toggler.maybe_go_left,
right_arrow: toggler.maybe_go_right,
},
});
}
$(".informational-overlays .overlay-tabs").append(elem);
common.adjust_mac_shortcuts(".hotkeys_table .hotkey kbd");
common.adjust_mac_shortcuts("#markdown-instructions kbd");
}
export function show(target) {
if (!toggler) {
set_up_toggler();
}
const overlay = $(".informational-overlays");
if (!overlay.hasClass("show")) {
overlays.open_overlay({
name: "informationalOverlays",
overlay,
on_close() {
browser_history.exit_overlay();
},
});
}
if (target) {
toggler.goto(target);
}
}
export function maybe_show_keyboard_shortcuts() {
if (overlays.is_active()) {
return;
}
if (popovers.any_active()) {
return;
}
show("keyboard-shortcuts");
}