# Code and handwriting

Source: https://mroffice.ai/en/developers/embed/code-and-handwriting

Besides Office files and PDF, an embedded editor opens source files in MR Office's Code editor and handwritten notes in its Handwriting editor: the same editors as in MR Office, with the same tokens, sessions, saves, signed callbacks (document.family "code" or "ink"), versions, retention, events and billing as any other file.

| Format | Opens in | Saved back as |
| --- | --- | --- |
| .py .pyw .js .mjs .cjs .jsx .ts .tsx .mts .cts .json .css .scss .sql .sh .bash .yaml .yml .toml .ini .xml .java .c .h .cpp .cc .cxx .hpp .hh .hxx .cs .go .rs .rb .php .swift .kt .kts .r .pl .pm .lua .tex | Code editor, with syntax highlighting (Kotlin as plain text, SCSS as CSS) | Itself: the same encoding (UTF-8, UTF-8 with a BOM, UTF-16 with its BOM) and line endings (LF or CRLF). A file nobody changed is the file it was. |
| .mrink | Handwriting editor: pens, pencils, highlighters, erasers, lasso, ruler and tape on handwriting paper | .mrink: the MR Office document JSON the open-source ink editor (@mrnote/ink) writes, { "format": "mrnote.document", "version": 1, "title", "content" }, its strokes included |

> .html, .txt and .md still open in the Word editor, and .csv in the Excel editor.

### A new document

A token with file.new: true and a file.name (no url or upload_id) opens a blank document of that format the first time its key is used: a handwriting document on dot-grid A4 paper for .mrink, an empty file for a code file, an empty document, workbook or deck for .docx, .xlsx and .pptx. Later tokens for the key join it. You have no copy yet, so its first save is sent to you as version 1.

```js
import { createSessionToken } from "mroffice-embed/server";

// A code file from your storage, saved back as itself (.py, its own encoding and line endings)
const codeToken = await createSessionToken({
  appId, secretKey, user,
  file: { key: "report-py", name: "report.py", url: signedUrlOf("report.py") },
});

// A new handwriting document: no file yet; its first save sends you "Lecture notes.mrink"
const inkToken = await createSessionToken({
  appId, secretKey, user,
  file: { key: `notes-${user.id}`, name: "Lecture notes.mrink", new: true },
});

// Let the Code editor run the file (an app on Pro and above; each run is one server job)
const runToken = await createSessionToken({
  appId, secretKey, user,
  file: { key: "report-py", name: "report.py", url: signedUrlOf("report.py") },
  features: { run: true },
});
```

### What each supports

|  | Code | Handwriting |
| --- | --- | --- |
| Modes | edit and view (comment, review and fillForms open read only) | edit and view (the same) |
| Save and callback | Yes: sha256, version, format ("py"…), converted: false | Yes: format "mrink" |
| Export (File › Export, document.export) | The file itself, in its own encoding and line endings, and HTML | mrink, PDF (paper and strokes as drawn) and Word (.docx) |
| Print | — | Yes (permissions.print) |
| permissions.download, export, copy; watermark | Yes | Yes |
| Comments, tracked changes, form fields | Not applicable | Not applicable |
| Version history and restore | Yes (ui.features.versionHistory) | Yes (ui.features.versionHistory) |
| Conversion API | — | mrink to pdf, png, jpg or docx |
| WOPI | No (embed.js only) | No (embed.js only) |

### Running code

- Running code is off in an embedded Code editor: there is no Run or Debug tab, and nothing a key starts runs. A run costs a sandbox on MR Office's servers.
- A token may turn it on with features: { run: true }, for an app on Pro or above (and Enterprise). On Free and Starter the claim is accepted and ignored: permissions.run in onReady says whether the session may run.
- Each run on MR Office's servers counts as one server job of your app, as a conversion does: billed as a session, within the plan's monthly jobs and per-minute rate. A run on MR Office's servers needs an edit session.

### Automation

```js
await editor.ready;

// A code file: its text as the editor holds it ("\n" line endings)
const { text, language } = await editor.code.getText();
await editor.code.insertText("# Reviewed by Acme\n", { location: "start" });
await editor.code.setSelection({ line: 3 }, { line: 5 });
const source = await editor.document.export();          // report.py, in its own encoding

// A handwriting document: its strokes, or erase them all (one Undo step)
const { count, strokes } = await editor.ink.getStrokes(); // [{ index, tool, color, widthPt }]
await editor.ink.clear();
const pdf = await editor.document.export("pdf");          // or "mrink", "docx"
```

### Limits

- A code file opens up to 2 MB and 1,000,000 characters (the Code editor's own limits), as UTF-8 or UTF-16 text without NUL bytes; anything else is refused with embed_file_not_readable (error.details.reason binary or not_text) or embed_file_too_large.
- An .mrink must be { "format": "mrnote.document", "version": 1, "content": { "type": "doc", … } } (error.details.reason not_json, not_mrink or unknown_version).
