Something went wrong. Try again.
Markdown editor
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298const std = @import("std");const Editor = @import("editor.zig").Editor;const tree = @import("piece_tree.zig");const inline_markdown = @import("inline_markdown.zig");const allocator = std.heap.wasm_allocator;var editor: Editor = .{ .allocator = allocator,};
// Track host-owned buffers so invalid frees cannot corrupt the editor heap.const Allocation = struct { bytes: []u8, next: ?*Allocation,};
var allocations: ?*Allocation = null;var inline_runs: []inline_markdown.Run = &.{};
fn clear_inline_runs() void { allocator.free(inline_runs); inline_runs = &.{};}
export fn inline_prepare(block_index: u32) u32 { clear_inline_runs(); const text = editor.block_text(block_index) orelse return 0; inline_runs = inline_markdown.parse(allocator, text, editor.state.blocks[block_index].kind == 8) catch return 0; return 1;}
// UINT32_MAX signals invalid input or allocation failure. These calls may// allocate: consume borrowed text/run buffers before invoking them.export fn inline_resolve(block_index: u32, offset: u32, bias: i32) u32 { return editor.inline_resolve(block_index, offset, bias) orelse std.math.maxInt(u32);}
// Boundary-only navigation: unchanged means the adapter must move a grapheme// (or block) itself. This never mutates the selection or inserts whitespace.export fn inline_step(block_index: u32, offset: u32, direction: i32) u32 { if (direction != -1 and direction != 1) { return std.math.maxInt(u32); } return editor.inline_resolve(block_index, offset, direction) orelse std.math.maxInt(u32);}
export fn inline_replace(block_index: u32, anchor: u32, focus: u32, ptr: u32, len: u32) u32 { return @intFromBool(editor.inline_replace(block_index, anchor, focus, input(ptr, len) orelse return 0));}
export fn inline_runs_ptr() u32 { if (inline_runs.len == 0) { return 0; } return @intCast(@intFromPtr(inline_runs.ptr));}
export fn inline_runs_len() u32 { return @intCast(inline_runs.len);}
export fn alloc(len: u32) u32 { if (len == 0) { return 0; } const bytes = allocator.alloc(u8, len) catch return 0; const node = allocator.create(Allocation) catch { allocator.free(bytes); return 0; }; node.* = .{ .bytes = bytes, .next = allocations, };
allocations = node; return @intCast(@intFromPtr(bytes.ptr));}
export fn free(ptr: u32, len: u32) void { var link = &allocations; while (link.*) |node| { if (@intFromPtr(node.bytes.ptr) == ptr and node.bytes.len == len) { link.* = node.next; allocator.free(node.bytes); allocator.destroy(node); return; } link = &node.next; }}
fn input(ptr: u32, len: u32) ?[]const u8 { if (len == 0) { return ""; } if (ptr == 0) { return null; } const end = @as(u64, ptr) + len; if (end > @as(u64, @wasmMemorySize(0)) * 65536) { return null; } return @as([*]const u8, @ptrFromInt(ptr))[0..len];}
export fn abi_version() u32 { return 6;}
export fn init() u32 { clear_inline_runs(); return @intFromBool(editor.init());}
export fn block_count() u32 { return @intCast(editor.state.blocks.len);}
export fn block_id(index: u32) u32 { if (index < editor.state.blocks.len) { return editor.state.blocks[index].id; } else { return 0; }}
export fn block_kind(index: u32) u32 { if (index < editor.state.blocks.len) { return editor.state.blocks[index].kind; } else { return 0; }}
export fn block_text_ptr(index: u32) u32 { const text = editor.block_text(index) orelse return 0; if (text.len == 0) { return 0; } else { return @intCast(@intFromPtr(text.ptr)); }}
export fn block_text_len(index: u32) u32 { if (index < editor.state.blocks.len) { return @intCast(tree.length(editor.state.blocks[index].root)); } else { return 0; }}
export fn set_selection(anchor_id: u32, anchor_offset: u32, focus_id: u32, focus_offset: u32) u32 { return @intFromBool(editor.set_selection(.{ .anchor = .{ .id = anchor_id, .offset = anchor_offset, }, .focus = .{ .id = focus_id, .offset = focus_offset, }, }));}
export fn selection_anchor_block() u32 { return editor.state.selection.anchor.id;}
export fn selection_anchor_offset() u32 { return editor.state.selection.anchor.offset;}
export fn selection_focus_block() u32 { return editor.state.selection.focus.id;}
export fn selection_focus_offset() u32 { return editor.state.selection.focus.offset;}
export fn replace_selection(ptr: u32, len: u32) u32 { return @intFromBool(editor.replace_selection(input(ptr, len) orelse return 0));}
export fn replace_range(anchor_id: u32, anchor_offset: u32, focus_id: u32, focus_offset: u32, ptr: u32, len: u32) u32 { return @intFromBool(editor.replace_range( .{ .anchor = .{ .id = anchor_id, .offset = anchor_offset, }, .focus = .{ .id = focus_id, .offset = focus_offset, }, }, input(ptr, len) orelse return 0, ));}
export fn type_text(anchor_id: u32, anchor_offset: u32, focus_id: u32, focus_offset: u32, ptr: u32, len: u32) u32 { return @intFromBool(editor.type_text( input(ptr, len) orelse return 0, .{ .anchor = .{ .id = anchor_id, .offset = anchor_offset, }, .focus = .{ .id = focus_id, .offset = focus_offset, }, }, ));}
export fn backspace_boundary() u32 { return @intFromBool(editor.backspace_boundary() orelse return 2);}
export fn set_kind(index: u32, kind: u32) u32 { return @intFromBool(editor.set_kind(index, kind));}
export fn undo() u32 { return @intFromBool(editor.undo());}
export fn redo() u32 { return @intFromBool(editor.redo());}
export fn revision() u32 { return editor.revision;}
export fn import_markdown(ptr: u32, len: u32) u32 { return @intFromBool(editor.import_markdown(input(ptr, len) orelse return 0));}
export fn source_ptr() u32 { const text = editor.source_text() orelse return 0; if (text.len == 0) { return 0; } else { return @intCast(@intFromPtr(text.ptr)); }}
export fn source_len() u32 { return @intCast((editor.source_text() orelse return 0).len);}
export fn markdown_ptr() u32 { const text = editor.serialize() orelse return 0; if (text.len == 0) { return 0; } else { return @intCast(@intFromPtr(text.ptr)); }}
export fn markdown_len() u32 { return @intCast((editor.serialize() orelse return 0).len);}
export fn shape_changed() u32 { return @intFromBool(editor.shape);}
export fn changed_block_count() u32 { return @intCast(editor.changed_ids.len);}
export fn changed_block_id(index: u32) u32 { if (index < editor.changed_ids.len) { return editor.changed_ids[index]; } else { return 0; }}
export fn block_depth(index: u32) u32 { if (index < editor.state.blocks.len) { return editor.state.blocks[index].depth; } else { return 0; }}
export fn block_ordinal(index: u32) u32 { if (index < editor.state.blocks.len) { return editor.state.blocks[index].ordinal; } else { return 0; }}