Last active
February 21, 2025 04:28
-
-
Save Quorafind/c380d2eb9d32326715b82950d0ec1fe9 to your computer and use it in GitHub Desktop.
Toggle blocks in editor editable or readonly
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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