Ink-away.koplugin
Examples

Event-only consumer

React to annotation opening and closing. No viewer or surface is needed.

YourPlugin is your plugin object. The helper names below belong to the example: call self:connectInkAway(self.ui) from your own reader setup, and self:dispose() from your teardown. They are not KOReader lifecycle signatures.

"connectInkAway(ink)": Its a mandatory prop your plugin must implement, Ink-away will call it when it loads in memory, it will install/configure your integration with ink-away

Example:

-- YourPlugin/main.lua -- illustrative mock, NOT an installable KOReader plugin.
-- Event-only consumer: handles both before-ready and after-ready registration.
-- No viewer, surface, rendering provider or rendering injection is required.
-- A real plugin wires these mock methods into its reader-scoped lifecycle.
local YourPlugin = {}

-- MOCK signature: ui is this reader's UI. There are no viewer arguments.
function YourPlugin:init(ui)
    self.ui = ui
    self.closed, self.annotation_open = false, false
    self.status = "Waiting for Ink Away"
    if ui.inkAwayIntegrationAPI then
        -- INK-AWAY has INITIALIZED: configure immediately using the ready instance.
        self:configureInkAway(ui.inkAwayIntegrationAPI)
    else
        -- BEFORE INK-AWAY IS INITIALIZED: share the queue without overwriting other consumers
        -- in case there are other plugins installed which integrate with ink-away as well!
        ui.inkAwayPendingIntegrations = ui.inkAwayPendingIntegrations or {}
        ui.inkAwayPendingIntegrations[self] = true
    end
end

function YourPlugin:configureInkAway(ink)
    if self.closed or self.stop_open then
        return
    end
    -- Event consumers only need the event API, not document_surface_version.
    if
        ink.integration_api_version ~= 1
        or type(ink.onAnnotationOpened) ~= "function"
        or type(ink.onAnnotationClosed) ~= "function"
    then
        self.status = "This Ink Away version does not support the event API"
        return
    end

    -- Observe all annotation sessions on this reader, including the native view.
    self.stop_open = ink:onAnnotationOpened(function(event)
        self.annotation_open = true
        self.annotation_document = event.document
        self.status = "Annotation opened"
        -- MOCK: pause your plugin's own work while annotation is active.
        -- event.surface may be nil (native viewer) or an external surface.
    end)
    self.stop_close = ink:onAnnotationClosed(function(event)
        self.last_annotation_document = event.document
        self.annotation_open = false
        self.annotation_document = nil
        self.status = "Annotation closed"
        -- MOCK: resume your plugin's work. This does not confirm a disk save.
    end)
    self.status = "Listening for annotation events"
end

-- Call when this plugin's reader-scoped instance is disposed.
function YourPlugin:dispose()
    if self.closed then
        return
    end
    self.closed = true
    local pending = self.ui.inkAwayPendingIntegrations
    if pending and pending[self] == true then
        pending[self] = nil
        if next(pending) == nil then
            self.ui.inkAwayPendingIntegrations = nil
        end
    end
    if self.stop_open then
        self.stop_open()
    end
    if self.stop_close then
        self.stop_close()
    end
    -- An observer owns no annotation surface and must not release someone else's.
    self.stop_open, self.stop_close = nil, nil
    self.annotation_open, self.annotation_document = false, nil
    self.status = "Disposed"
end

return YourPlugin

Desglosing the previous example, would be:

1. Connect when your reader opens

function YourPlugin:connectInkAway(ui)
    self.ui = ui
    self.closed = false
    if ui.inkAwayIntegrationAPI then
        self:configureInkAway(ui.inkAwayIntegrationAPI)
    else
        ui.inkAwayPendingIntegrations = ui.inkAwayPendingIntegrations or {}
        ui.inkAwayPendingIntegrations[self] = true
    end
end

If Ink Away is already ready, configuration happens immediately. If it loads later, it removes your queue entry and calls your method. You do not need to look up ui["ink-away"] or control plugin loading order.

2. Subscribe in configureInkAway

function YourPlugin:configureInkAway(ink)
    if self.closed or self.stop_open then return end
    if ink.integration_api_version ~= 1 then return end

    self.stop_open = ink:onAnnotationOpened(function(event)
        self.annotation_open = true
        self.annotation_document = event.document
        -- Pause your plugin's work here.
    end)
    self.stop_close = ink:onAnnotationClosed(function()
        self.annotation_open = false
        self.annotation_document = nil
        -- Resume your plugin's work here.
    end)
end

Keep the two returned functions: calling them unsubscribes your listeners. event.document identifies the book. event.surface is nil for the normal reader, or a surface table for a custom viewer. Events describe future openings and closings; subscribing does not replay a session already in progress.

3. Disconnect when your owner closes

function YourPlugin:dispose()
    if self.closed then return end
    self.closed = true
    local pending = self.ui.inkAwayPendingIntegrations
    if pending then pending[self] = nil end
    if self.stop_open then self.stop_open() end
    if self.stop_close then self.stop_close() end
    self.stop_open, self.stop_close = nil, nil
end

Removing the queue entry cancels a connection that has not happened yet. Unsubscribing stops an existing connection. An event-only consumer does not close an editor or release another plugin's surface.

Download the complete Lua mock.

For details, see onAnnotationOpened, onAnnotationClosed, and the pending queue.

On this page