Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/packages/frontend/codemirror/extensions/insert-special-char.tsx
Views: 687
/*1* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45import * as CodeMirror from "codemirror";6import { Button, Modal } from "antd";78import { alert_message } from "../../alerts";9import { show_react_modal } from "../../misc";1011import emojis from "markdown-it-emoji/lib/data/full.json";1213export interface Options {14char: string; // utf8 representation of the symbol15html?: string; // html code for the symbol (without "&...;" around it)16markdown?: string; // markdown for the symbol (without ":...:" around it)17}1819export async function get_insert_special_char_from_user(): Promise<20undefined | Options21> {22return await show_react_modal((cb) => {23const style = {24width: "1.3em",25paddingLeft: ".3em",26display: "inline-block",27cursor: "pointer",28};29const symbols: JSX.Element[] = SYMBOLS.map((symbol, i) => (30<span31key={i}32style={style}33onClick={() => cb(undefined, symbol)}34>35{symbol.char}36</span>37));38return (39<Modal40title={<h3>Ω Insert Special Symbol or Emoji</h3>}41open42footer={<Button onClick={() => cb()}>Cancel</Button>}43onCancel={() => cb()}44centered45width={"70vw"}46>47<div48className="webapp-html-editor-symbols-dialog"49style={{ fontSize: "20pt" }}50>51{symbols}52</div>53</Modal>54);55});56}5758function insert_special_char(mode: string, opts: Options): string {59if (mode == "html" && opts.html) {60return `&${opts.html};`;61}62if (mode == "md" && opts.markdown) {63return `:${opts.markdown}:`;64}65// This fallback should work wherever utf8 works,66// which is pretty general these days.67return opts.char;68}6970CodeMirror.defineExtension(71"insert_special_char",72async function (): Promise<void> {73// @ts-ignore74const cm = this;75let opts: Options | undefined = undefined;7677try {78opts = await get_insert_special_char_from_user();79} catch (err) {80alert_message({ type: "error", message: err.toString() });81return;82}8384if (opts == null) {85return; // user canceled86}8788const selections = cm.listSelections();89selections.reverse();90for (const sel of selections) {91const link = insert_special_char(cm.get_edit_mode(sel.head), opts);92if (sel.empty()) {93cm.replaceRange(link, sel.head);94} else {95cm.replaceRange(link, sel.from(), sel.to());96}97}98}99);100101const SYMBOLS: Options[] = [102{ html: "Aacute", char: "Á" },103{ html: "aacute", char: "á" },104{ html: "Acirc", char: "Â" },105{ html: "acirc", char: "â" },106{ html: "acute", char: "´" },107{ html: "AElig", char: "Æ" },108{ html: "aelig", char: "æ" },109{ html: "Agrave", char: "À" },110{ html: "agrave", char: "à" },111{ html: "alefsym", char: "ℵ" },112{ html: "Alpha", char: "Α" },113{ html: "alpha", char: "α" },114{ html: "amp", char: "&" },115{ html: "and", char: "∧" },116{ html: "ang", char: "∠" },117{ html: "Aring", char: "Å" },118{ html: "aring", char: "å" },119{ html: "asymp", char: "≈" },120{ html: "Atilde", char: "Ã" },121{ html: "atilde", char: "ã" },122{ html: "Auml", char: "Ä" },123{ html: "auml", char: "ä" },124{ html: "bdquo", char: "„" },125{ html: "Beta", char: "Β" },126{ html: "beta", char: "β" },127{ html: "brvbar", char: "¦" },128{ html: "bull", char: "•" },129{ html: "cap", char: "∩" },130{ html: "Ccedil", char: "Ç" },131{ html: "ccedil", char: "ç" },132{ html: "cedil", char: "¸" },133{ html: "cent", char: "¢" },134{ html: "Chi", char: "Χ" },135{ html: "chi", char: "χ" },136{ html: "circ", char: "ˆ" },137{ html: "clubs", char: "♣" },138{ html: "cong", char: "≅" },139{ html: "copy", char: "©" },140{ html: "crarr", char: "↵" },141{ html: "cup", char: "∪" },142{ html: "curren", char: "¤" },143{ html: "dagger", char: "†" },144{ html: "Dagger", char: "‡" },145{ html: "darr", char: "↓" },146{ html: "dArr", char: "⇓" },147{ html: "deg", char: "°" },148{ html: "Delta", char: "Δ" },149{ html: "delta", char: "δ" },150{ html: "diams", char: "♦" },151{ html: "divide", char: "÷" },152{ html: "Eacute", char: "É" },153{ html: "eacute", char: "é" },154{ html: "Ecirc", char: "Ê" },155{ html: "ecirc", char: "ê" },156{ html: "Egrave", char: "È" },157{ html: "egrave", char: "è" },158{ html: "empty", char: "∅" },159{ html: "Epsilon", char: "Ε" },160{ html: "epsilon", char: "ε" },161{ html: "equiv", char: "≡" },162{ html: "Eta", char: "Η" },163{ html: "eta", char: "η" },164{ html: "ETH", char: "Ð" },165{ html: "eth", char: "ð" },166{ html: "Euml", char: "Ë" },167{ html: "euml", char: "ë" },168{ html: "euro", char: "€" },169{ html: "exist", char: "∃" },170{ html: "fnof", char: "ƒ" },171{ html: "forall", char: "∀" },172{ html: "frac12", char: "½" },173{ html: "frac14", char: "¼" },174{ html: "frac34", char: "¾" },175{ html: "frasl", char: "⁄" },176{ html: "Gamma", char: "Γ" },177{ html: "gamma", char: "γ" },178{ html: "ge", char: "≥" },179{ html: "gt", char: "<" },180{ html: "harr", char: "↔" },181{ html: "hArr", char: "⇔" },182{ html: "hearts", char: "♥" },183{ html: "hellip", char: "…" },184{ html: "Iacute", char: "Í" },185{ html: "iacute", char: "í" },186{ html: "Icirc", char: "Î" },187{ html: "icirc", char: "î" },188{ html: "iexcl", char: "¡" },189{ html: "Igrave", char: "Ì" },190{ html: "igrave", char: "ì" },191{ html: "image", char: "ℑ" },192{ html: "infin", char: "∞" },193{ html: "int", char: "∫" },194{ html: "Iota", char: "Ι" },195{ html: "iota", char: "ι" },196{ html: "iquest", char: "¿" },197{ html: "isin", char: "∈" },198{ html: "Iuml", char: "Ï" },199{ html: "iuml", char: "ï" },200{ html: "Kappa", char: "Κ" },201{ html: "kappa", char: "κ" },202{ html: "Lambda", char: "Λ" },203{ html: "lambda", char: "λ" },204{ html: "lang", char: "〈" },205{ html: "laquo", char: "«" },206{ html: "larr", char: "←" },207{ html: "lArr", char: "⇐" },208{ html: "lceil", char: "⌈" },209{ html: "ldquo", char: "“" },210{ html: "le", char: "≤" },211{ html: "lfloor", char: "⌊" },212{ html: "lowast", char: "∗" },213{ html: "loz", char: "◊" },214{ html: "lsaquo", char: "‹" },215{ html: "lsquo", char: "‘" },216{ html: "lt", char: "<" },217{ html: "macr", char: "¯" },218{ html: "mdash", char: "—" },219{ html: "micro", char: "µ" },220{ html: "middot", char: "·" },221{ html: "minus", char: "−" },222{ html: "Mu", char: "Μ" },223{ html: "mu", char: "μ" },224{ html: "nabla", char: "∇" },225{ html: "ndash", char: "–" },226{ html: "ne", char: "≠" },227{ html: "ni", char: "∋" },228{ html: "not", char: "¬" },229{ html: "notin", char: "∉" },230{ html: "nsub", char: "⊄" },231{ html: "Ntilde", char: "Ñ" },232{ html: "ntilde", char: "ñ" },233{ html: "Nu", char: "Ν" },234{ html: "nu", char: "ν" },235{ html: "Oacute", char: "Ó" },236{ html: "oacute", char: "ó" },237{ html: "Ocirc", char: "Ô" },238{ html: "ocirc", char: "ô" },239{ html: "OElig", char: "Œ" },240{ html: "oelig", char: "œ" },241{ html: "Ograve", char: "Ò" },242{ html: "ograve", char: "ò" },243{ html: "oline", char: "‾" },244{ html: "Omega", char: "Ω" },245{ html: "omega", char: "ω" },246{ html: "Omicron", char: "Ο" },247{ html: "omicron", char: "ο" },248{ html: "oplus", char: "⊕" },249{ html: "or", char: "∨" },250{ html: "ordf", char: "ª" },251{ html: "ordm", char: "º" },252{ html: "Oslash", char: "Ø" },253{ html: "oslash", char: "ø" },254{ html: "Otilde", char: "Õ" },255{ html: "otilde", char: "õ" },256{ html: "otimes", char: "⊗" },257{ html: "Ouml", char: "Ö" },258{ html: "ouml", char: "ö" },259{ html: "para", char: "¶" },260{ html: "part", char: "∂" },261{ html: "permil", char: "‰" },262{ html: "perp", char: "⊥" },263{ html: "Phi", char: "Φ" },264{ html: "phi", char: "φ" },265{ html: "Pi", char: "Π" },266{ html: "pi", char: "π" },267{ html: "piv", char: "ϖ" },268{ html: "plusmn", char: "±" },269{ html: "pound", char: "£" },270{ html: "prime", char: "′" },271{ html: "Prime", char: "″" },272{ html: "prod", char: "∏" },273{ html: "prop", char: "∝" },274{ html: "Psi", char: "Ψ" },275{ html: "psi", char: "ψ" },276{ html: "quot", char: '"' },277{ html: "radic", char: "√" },278{ html: "rang", char: "〉" },279{ html: "raquo", char: "»" },280{ html: "rarr", char: "→" },281{ html: "rArr", char: "⇒" },282{ html: "rceil", char: "⌉" },283{ html: "rdquo", char: "”" },284{ html: "real", char: "ℜ" },285{ html: "reg", char: "®" },286{ html: "rfloor", char: "⌋" },287{ html: "Rho", char: "Ρ" },288{ html: "rho", char: "ρ" },289{ html: "rsaquo", char: "›" },290{ html: "rsquo", char: "’" },291{ html: "sbquo", char: "‚" },292{ html: "Scaron", char: "Š" },293{ html: "scaron", char: "š" },294{ html: "sdot", char: "⋅" },295{ html: "sect", char: "§" },296{ html: "Sigma", char: "Σ" },297{ html: "sigma", char: "σ" },298{ html: "sigmaf", char: "ς" },299{ html: "sim", char: "∼" },300{ html: "spades", char: "♠" },301{ html: "sub", char: "⊂" },302{ html: "sube", char: "⊆" },303{ html: "sum", char: "∑" },304{ html: "sup", char: "⊃" },305{ html: "sup1", char: "¹" },306{ html: "sup2", char: "²" },307{ html: "sup3", char: "³" },308{ html: "supe", char: "⊇" },309{ html: "szlig", char: "ß" },310{ html: "Tau", char: "Τ" },311{ html: "tau", char: "τ" },312{ html: "there4", char: "∴" },313{ html: "Theta", char: "Θ" },314{ html: "theta", char: "θ" },315{ html: "thetasym", char: "ϑ" },316{ html: "THORN", char: "Þ" },317{ html: "thorn", char: "þ" },318{ html: "tilde", char: "˜" },319{ html: "times", char: "×" },320{ html: "trade", char: "™" },321{ html: "Uacute", char: "Ú" },322{ html: "uacute", char: "ú" },323{ html: "uarr", char: "↑" },324{ html: "uArr", char: "⇑" },325{ html: "Ucirc", char: "Û" },326{ html: "ucirc", char: "û" },327{ html: "Ugrave", char: "Ù" },328{ html: "ugrave", char: "ù" },329{ html: "uml", char: "¨" },330{ html: "upsih", char: "ϒ" },331{ html: "Upsilon", char: "Υ" },332{ html: "upsilon", char: "υ" },333{ html: "Uuml", char: "Ü" },334{ html: "uuml", char: "ü" },335{ html: "weierp", char: "℘" },336{ html: "Xi", char: "Ξ" },337{ html: "xi", char: "ξ" },338{ html: "Yacute", char: "Ý" },339{ html: "yacute", char: "ý" },340{ html: "yen", char: "¥" },341{ html: "yuml", char: "ÿ" },342{ html: "Yuml", char: "Ÿ" },343{ html: "Zeta", char: "Ζ" },344{ html: "zeta", char: "ζ" },345{ html: "zwj", char: "" },346{ html: "zwnj", char: "" },347] as Options[];348349const seen = new Set<string>();350for (const markdown in emojis) {351const x = emojis[markdown];352if (seen.has(x)) continue;353seen.add(x);354SYMBOLS.push({ markdown, char: emojis[markdown] });355}356357358