Skip to content

Commit 790066b

Browse files
committed
fix(commands): add non-nested version for remove new lines command
1 parent 7257c68 commit 790066b

File tree

2 files changed

+34
-25
lines changed

2 files changed

+34
-25
lines changed

src/app.ts

+8-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {
1717
joinAsSentences_Map, joinViaCommas_Attach, joinViaSpaces_Attach,
1818
joinViaNewLines_Attach, joinViaNewLines_Map,
1919

20-
splitByLines, splitBySentences, splitByWords, removeNewLinesCommand,
20+
splitByLines, splitBySentences, splitByWords, removeNewLines,
2121
} from './commands'
2222
import { improveCursorMovement_KeyDownListener, improveSearch_KeyDownListener } from './features'
2323
import { getChosenBlocks, p, scrollToBlock } from './utils'
@@ -215,10 +215,15 @@ async function main() {
215215

216216
// Updates
217217
logseq.App.registerCommandPalette({
218-
label: ICON + ' Remove new lines', key: 'mc-6-update-1-remove-new-lines',
218+
label: ICON + ' Remove new lines', key: 'mc-7-update-1-remove-new-lines',
219219
// @ts-expect-error
220220
keybinding: {},
221-
}, (e) => removeNewLinesCommand())
221+
}, (e) => updateBlocksCommand(removeNewLines))
222+
logseq.App.registerCommandPalette({
223+
label: ICON + ' Remove new lines (with nested)', key: 'mc-7-update-2-remove-new-lines-nested',
224+
// @ts-expect-error
225+
keybinding: {},
226+
}, (e) => updateBlocksCommand(removeNewLines, true))
222227

223228

224229
// Navigation

src/commands.ts

+26-22
Original file line numberDiff line numberDiff line change
@@ -848,38 +848,44 @@ export function magicJoinCommand(independentMode: boolean) {
848848

849849
export async function updateBlocksCommand(
850850
callback: (content: string, level: number, block: BlockEntity, parent?: BlockEntity) => string,
851+
recursive: boolean = false,
851852
) {
852853
let [ blocks, isSelectedState ] = await getChosenBlocks()
853854
if (blocks.length === 0)
854855
return
855856

856857
blocks = unique(blocks, (b) => b.uuid)
857-
blocks = await Promise.all(
858-
blocks.map(async (b) => {
859-
return (
860-
await logseq.Editor.getBlock(b.uuid, {includeChildren: true})
861-
)!
862-
})
863-
)
864-
blocks = filterOutChildBlocks(blocks)
858+
if (recursive) {
859+
blocks = await Promise.all(
860+
blocks.map(async (b) => {
861+
return (
862+
await logseq.Editor.getBlock(b.uuid, {includeChildren: true})
863+
)!
864+
})
865+
)
866+
blocks = filterOutChildBlocks(blocks)
867+
}
865868

866869
if (blocks.length === 0)
867870
return
868871

869-
870872
// it is important to check if any block in the tree has references
871873
// (Logseq replaces references with it's text)
872-
for (const block of blocks) {
873-
const blocksWithReferences = await getBlocksWithReferences(block)
874-
block._treeHasReferences = blocksWithReferences.length !== 0
875-
}
876-
874+
if (recursive)
875+
for (const block of blocks) {
876+
const blocksWithReferences = await getBlocksWithReferences(block)
877+
block._treeHasReferences = blocksWithReferences.length !== 0
878+
}
877879

878880
for (const block of blocks) {
879-
// ensure .children always is array
881+
// ensure .children is always an array
880882
if (!block.children)
881883
block.children = []
882884

885+
// skip child nodes in non-recursive mode
886+
if (!recursive)
887+
block.children = []
888+
883889
const newTree = walkBlockTree(block as WalkBlock, (b, level, p, data) => {
884890
const properties = PropertiesUtils.fromCamelCaseAll(data.node.properties)
885891
const propertiesOrder = PropertiesUtils.getPropertyNames(b.content)
@@ -909,11 +915,9 @@ export async function updateBlocksCommand(
909915
}
910916
}
911917

912-
export function removeNewLinesCommand() {
913-
return updateBlocksCommand((content, level, block, parent) => {
914-
return content
915-
.replaceAll(/\n+/g, '\n')
916-
.replaceAll(/(?<=[^\S\n])\n/g, '') // remove \n when there are spaces before
917-
.replaceAll(/(?<![^\S])\n/g, ' ') // replace \n to space when there are no spaces before
918-
})
918+
export function removeNewLines(content, level, block, parent) {
919+
return content
920+
.replaceAll(/\n+/g, '\n')
921+
.replaceAll(/(?<=[^\S\n])\n/g, '') // remove \n when there are spaces before
922+
.replaceAll(/(?<![^\S])\n/g, ' ') // replace \n to space when there are no spaces before
919923
}

0 commit comments

Comments
 (0)