Skip to content

Commit

Permalink
feat: ✨ add expand on button
Browse files Browse the repository at this point in the history
  • Loading branch information
mrjackphil committed Jun 9, 2022
1 parent 6a80151 commit 03dfca2
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,13 @@ export default class TextExpander extends Plugin {
async onload() {
this.addSettingTab(new SettingTab(this.app, this));

this.registerMarkdownCodeBlockProcessor('expander', (source, el, ctx) => {
el
.createDiv()
.createEl('button', { text: 'Run expand query', })
.addEventListener('click', this.init.bind(this, false, ctx.getSectionInfo(el).lineStart))
})

this.addCommand({
id: 'editor-expand',
name: 'expand',
Expand Down Expand Up @@ -134,7 +141,7 @@ export default class TextExpander extends Plugin {
await this.saveData(this.config)
}

private async init(proceedAllQueriesOnPage = false) {
private async init(proceedAllQueriesOnPage = false, lineToStart?: number) {
const currentView = this.app.workspace.activeLeaf.view

// Is on editable view
Expand All @@ -144,9 +151,11 @@ export default class TextExpander extends Plugin {

const cmDoc: Editor = this.cm = currentView.editor

const curNum = cmDoc.getCursor().line
const curNum = lineToStart || cmDoc.getCursor().line
const content = cmDoc.getValue()

cmDoc.setCursor(lineToStart ? lineToStart - 1 : 0)

const formatted = splitByLines(content)
let findQueries = getAllExpandersQuery(formatted)
const closestQuery = getClosestQuery(findQueries, curNum)
Expand Down Expand Up @@ -198,7 +207,7 @@ export default class TextExpander extends Plugin {
let searchResults: Map<TFile, SearchDetails> | undefined = undefined
let files: TFile[] = []

if(query.query !== '') {
if (query.query !== '') {
searchResults = await this.getFoundAfterDelay()

files = extractFilesFromSearchResults(searchResults, currentFileName, this.config.excludeCurrent);
Expand Down Expand Up @@ -233,7 +242,6 @@ export default class TextExpander extends Plugin {
this.config.lineEnding
].filter(e => e).join('\n')


// Do not paste generated content if used changed activeLeaf
const viewBeforeReplace = this.app.workspace.activeLeaf.view
if (!(viewBeforeReplace instanceof MarkdownView) || viewBeforeReplace.file.basename !== currentFileName) {
Expand All @@ -248,7 +256,9 @@ export default class TextExpander extends Plugin {
}

private async generateTemplateFromSequences(files: TFile[], repeatableContent: string[], searchResults?: Map<TFile, SearchDetails>): Promise<string> {
if (!searchResults) { return '' }
if (!searchResults) {
return ''
}

const changed = await Promise.all(
files
Expand Down

0 comments on commit 03dfca2

Please sign in to comment.