Skip to content

Instantly share code, notes, and snippets.

@Quorafind
Last active February 21, 2025 04:28
Show Gist options
  • Save Quorafind/c380d2eb9d32326715b82950d0ec1fe9 to your computer and use it in GitHub Desktop.
Save Quorafind/c380d2eb9d32326715b82950d0ec1fe9 to your computer and use it in GitHub Desktop.
Toggle blocks in editor editable or readonly
module.exports = async params => {
// Get current active file view
const activeView = params.app.workspace.getActiveFileView()
if (!activeView) {
new Notice('No active file view found!')
return
}
// Toggle edit mode for all blocks
const toggleBlockEditMode = (blocks, isEditing) => {
for (let block of blocks) {
try {
block.editable = isEditing
isEditing ? block.showEditor() : block.showPreview()
} catch (error) {
console.error(`Error ${isEditing ? 'showing editor' : 'showing preview'} for block:`, error)
}
}
}
// Check if isEditingBlock property exists
if (typeof activeView.isEditingBlock === 'undefined') {
// First run - set all blocks to editable
activeView.isEditingBlock = true
toggleBlockEditMode(activeView.editMode._children.filter(i=>i.editorEl), true)
} else {
// Toggle between edit and preview mode based on current state
const newEditingState = !activeView.isEditingBlock
toggleBlockEditMode(activeView.editMode._children.filter(i=>i.editorEl), newEditingState)
activeView.isEditingBlock = newEditingState
}
// Show current status notification
new Notice(`Edit mode: ${activeView.isEditingBlock ? 'Editor' : 'Preview'}`)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment