You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

868 lines
144 KiB

3 years ago
'use strict';
var obsidian = require('obsidian');
3 years ago
/******************************************************************************
3 years ago
Copyright (c) Microsoft Corporation.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */
/* global Reflect, Promise */
var extendStatics = function(d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
function __extends(d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
}
var __assign = function() {
__assign = Object.assign || function __assign(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
function __awaiter(thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
}
function __generator(thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
}
3 years ago
function __values(o) {
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
if (m) return m.call(o);
if (o && typeof o.length === "number") return {
next: function () {
if (o && i >= o.length) o = void 0;
return { value: o && o[i++], done: !o };
}
};
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
}
3 years ago
function __read(o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
if (!m) return o;
var i = m.call(o), r, ar = [], e;
try {
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
}
catch (error) { e = { error: error }; }
finally {
try {
if (r && !r.done && (m = i["return"])) m.call(i);
}
finally { if (e) throw e.error; }
}
return ar;
}
function __spreadArray(to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
3 years ago
return to.concat(ar || Array.prototype.slice.call(from));
3 years ago
}
function formatContent(content) {
return content.split('\n');
}
function getAllExpandersQuery(content) {
var accum = [];
for (var i = 0; i < content.length; i++) {
var line = content[i];
if (line === '```expander') {
for (var e = 0; e < content.length - i; e++) {
var nextline = content[i + e];
if (nextline === '```') {
accum.push({
start: i,
end: i + e,
query: content[i + 1],
template: e > 2 ? content.slice(i + 2, i + e).join('\n') : ''
});
break;
}
}
}
}
return accum;
}
function getClosestQuery(queries, lineNumber) {
if (queries.length === 0) {
return undefined;
}
return queries.reduce(function (a, b) {
return Math.abs(b.start - lineNumber) < Math.abs(a.start - lineNumber) ? b : a;
});
}
function getLastLineToReplace(content, query, endline) {
var lineFrom = query.end;
for (var i = lineFrom + 1; i < content.length; i++) {
if (content[i] === endline) {
return i;
}
}
return lineFrom + 1;
}
function trimContent(s) {
var removeEmptyLines = function (s) {
var lines = s.split('\n').map(function (e) { return e.trim(); });
if (lines.length < 2) {
return s;
}
if (lines.indexOf('') === 0) {
return removeEmptyLines(lines.slice(1).join('\n'));
}
return s;
};
var removeFrontMatter = function (s, lookEnding) {
if (lookEnding === void 0) { lookEnding = false; }
var lines = s.split('\n');
if (lookEnding && lines.indexOf('---') === 0) {
return lines.slice(1).join('\n');
}
if (lookEnding) {
return removeFrontMatter(lines.slice(1).join('\n'), true);
}
if (lines.indexOf('---') === 0) {
return removeFrontMatter(lines.slice(1).join('\n'), true);
}
return s;
};
return removeFrontMatter(removeEmptyLines(s));
}
3 years ago
function highlight(lineStart, lineEnd, matchStart, matchEnd, lineContent) {
return __spreadArray(__spreadArray(__spreadArray(__spreadArray(__spreadArray([], __read(lineContent.slice(0, matchStart - lineStart)), false), [
'=='
], false), __read(lineContent.slice(matchStart - lineStart, (matchStart - lineStart) + (matchEnd - matchStart))), false), [
'=='
], false), __read(lineContent.slice((matchStart - lineStart) + (matchEnd - matchStart))), false).join('');
}
var sequences = [
{
name: '\\$count',
loop: true,
format: function (_p, _s, _content, _file, _d, index) { return index ? String(index + 1) : String(1); },
desc: 'add index number to each produced file'
},
{
name: '\\$filename',
loop: true,
format: function (_p, _s, _content, file) { return file.basename; },
desc: 'name of the founded file'
},
{
name: '\\$link',
loop: true,
format: function (p, _s, _content, file) { return p.app.fileManager.generateMarkdownLink(file, file.path); },
desc: 'link based on Obsidian settings'
},
{
name: '\\$lines:\\d+',
loop: true,
readContent: true,
format: function (p, s, content, _file) {
var digits = Number(s.split(':')[1]);
return trimContent(content)
.split('\n')
.filter(function (_, i) { return i < digits; })
.join('\n')
.replace(new RegExp(p.config.lineEnding, 'g'), '');
},
desc: 'specified count of lines from the found file'
},
{
name: '\\$characters:\\d+',
loop: true,
readContent: true,
format: function (p, s, content, _file) {
var digits = Number(s.split(':')[1]);
return trimContent(content)
.split('')
.filter(function (_, i) { return i < digits; })
.join('')
.replace(new RegExp(p.config.lineEnding, 'g'), '');
},
desc: 'specified count of lines from the found file'
},
{
name: '\\$frontmatter:[\\p\{L\}_-]+',
loop: true,
format: function (p, s, _content, file) { return p.getFrontMatter(s, file); },
desc: 'value from the frontmatter key in the found file'
},
{
name: '\\$lines+',
loop: true,
readContent: true,
format: function (p, s, content, _file) { return content.replace(new RegExp(p.config.lineEnding, 'g'), ''); },
desc: 'all content from the found file'
},
{
name: '\\$ext',
loop: true,
format: function (_p, s, content, file) { return file.extension; },
desc: 'return file extension'
},
{
name: '\\$created:format:date',
loop: true,
format: function (_p, s, content, file) { return String(new Date(file.stat.ctime).toISOString()).split('T')[0]; },
desc: 'created time formatted'
},
{
name: '\\$created:format:time',
loop: true,
format: function (_p, s, content, file) { return String(new Date(file.stat.ctime).toISOString()).split(/([.T])/)[2]; },
desc: 'created time formatted'
},
{
name: '\\$created:format',
loop: true,
format: function (_p, s, content, file) { return String(new Date(file.stat.ctime).toISOString()); },
desc: 'created time formatted'
},
{
name: '\\$created',
loop: true,
format: function (_p, s, content, file) { return String(file.stat.ctime); },
desc: 'created time'
},
{
name: '\\$size',
loop: true,
format: function (_p, s, content, file) { return String(file.stat.size); },
desc: 'size of the file'
},
{
name: '\\$path',
loop: true,
format: function (_p, s, content, file) { return file.path; },
desc: 'path to the found file'
},
{
name: '\\$parent',
loop: true,
format: function (_p, s, content, file) { return file.parent.name; },
desc: 'parent folder name'
},
{
name: '^(.+|)\\$header:.+',
loop: true,
format: function (p, s, content, file) {
var _a;
var prefix = s.slice(0, s.indexOf('$'));
var header = s.slice(s.indexOf('$')).replace('$header:', '').replace(/"/g, '');
var neededLevel = header.split("#").length - 1;
var neededTitle = header.replace(/^#+/g, '').trim();
var metadata = p.app.metadataCache.getFileCache(file);
return ((_a = metadata.headings) === null || _a === void 0 ? void 0 : _a.filter(function (e) {
var tests = [
[neededTitle, e.heading.includes(neededTitle)],
[neededLevel, e.level === neededLevel]
].filter(function (e) { return e[0]; });
if (tests.length) {
return tests.map(function (e) { return e[1]; }).every(function (e) { return e === true; });
}
return true;
}).map(function (h) { return p.app.fileManager.generateMarkdownLink(file, file.basename, '#' + h.heading); }).map(function (link) { return prefix + link; }).join('\n')) || '';
},
desc: 'headings from founded files. $header:## - return all level 2 headings. $header:Title - return all heading which match the string. Can be prepended like: - !$header:## to transclude the headings.'
},
{
name: '^(.+|)\\$blocks',
readContent: true,
loop: true,
format: function (p, s, content, file) {
var prefix = s.slice(0, s.indexOf('$'));
return content
.split('\n')
.filter(function (e) { return /\^\w+$/.test(e); })
.map(function (e) {
return prefix + p.app.fileManager.generateMarkdownLink(file, file.basename, '#' + e.replace(/^.+?(\^\w+$)/, '$1'));
})
.join('\n');
},
desc: 'block ids from the found files. Can be prepended.'
},
{
name: '^(.+|)\\$match:header', loop: true, format: function (p, s, content, file, results) {
var _a;
var prefix = s.slice(0, s.indexOf('$'));
var metadata = p.app.metadataCache.getFileCache(file);
var headings = (_a = metadata.headings) === null || _a === void 0 ? void 0 : _a.filter(function (h) { return results.result.content.filter(function (c) { return h.position.end.offset < c[0]; }).some(function (e) { return e; }); }).slice(-1);
return headings
.map(function (h) { return p.app.fileManager.generateMarkdownLink(file, file.path, '#' + h.heading); })
.map(function (link) { return prefix + link; })
.join('\n') || '';
}, desc: 'extract found selections'
},
{
name: '^(.+|)\\$matchline:(\\+|-|)\\d+',
loop: true,
format: function (_p, s, content, file, results) {
s.slice(0, s.indexOf('$matchline'));
var value = s.slice(s.indexOf('$matchline')).split(':')[1];
var isPlus = value.contains('+');
var isMinus = value.contains('-');
var isContext = !isPlus && !isMinus;
var offset = Number(value.replace(/[+-]/, ''));
var lines = results.content.split('\n');
// Grab info about line content, index, text length and start/end character position
var lineInfos = [];
for (var i = 0; i < lines.length; i++) {
var text = lines[i];
if (i === 0) {
lineInfos.push({
num: 0,
start: 0,
end: text.length,
text: text
});
continue;
}
var start = lineInfos[i - 1].end + 1;
lineInfos.push({
num: i,
start: start,
text: text,
end: text.length + start
});
}
return results.result.content.map(function (_a) {
var e_1, _b;
var _c = __read(_a, 2), from = _c[0], to = _c[1];
var matchedLines = lineInfos
.filter(function (_a) {
var start = _a.start, end = _a.end;
return start <= from && end >= to;
})
.map(function (line) {
return __assign(__assign({}, line), { text: highlight(line.start, line.end, from, to, line.text) });
});
var resultLines = [];
var _loop_1 = function (matchedLine) {
var prevLines = isMinus || isContext
? lineInfos.filter(function (l) { return matchedLine.num - l.num > 0 && matchedLine.num - l.num < offset; })
: [];
var nextLines = isPlus || isContext
? lineInfos.filter(function (l) { return l.num - matchedLine.num > 0 && l.num - matchedLine.num < offset; })
: [];
resultLines.push.apply(resultLines, __spreadArray(__spreadArray(__spreadArray([], __read(prevLines), false), [matchedLine], false), __read(nextLines), false));
};
try {
for (var matchedLines_1 = __values(matchedLines), matchedLines_1_1 = matchedLines_1.next(); !matchedLines_1_1.done; matchedLines_1_1 = matchedLines_1.next()) {
var matchedLine = matchedLines_1_1.value;
_loop_1(matchedLine);
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (matchedLines_1_1 && !matchedLines_1_1.done && (_b = matchedLines_1.return)) _b.call(matchedLines_1);
}
finally { if (e_1) throw e_1.error; }
}
return resultLines.map(function (e) { return e.text; }).join('\n');
}).join('\n');
}, desc: 'extract line with matches'
},
{
name: '^(.+|)\\$matchline', loop: true, format: function (_p, s, content, file, results) {
s.slice(0, s.indexOf('$'));
var lines = results.content.split('\n');
// Grab info about line content, index, text length and start/end character position
var lineInfos = [];
for (var i = 0; i < lines.length; i++) {
var text = lines[i];
if (i === 0) {
lineInfos.push({
num: 0,
start: 0,
end: text.length,
text: text
});
continue;
}
var start = lineInfos[i - 1].end + 1;
lineInfos.push({
num: i,
start: start,
text: text,
end: text.length + start
});
}
return results.result.content.map(function (_a) {
var _b = __read(_a, 2), from = _b[0], to = _b[1];
return lineInfos
.filter(function (_a) {
var start = _a.start, end = _a.end;
return start <= from && end >= to;
})
.map(function (_a) {
var start = _a.start, end = _a.end, text = _a.text;
return highlight(start, end, from, to, text);
}).join('\n');
}).join('\n');
}, desc: 'extract line with matches'
},
{
name: '^(.+|)\\$match', loop: true, format: function (_p, s, content, file, results) {
if (!results.result.content) {
console.warn('There is no content in results');
return '';
}
function appendPrefix(prefix, line) {
return prefix + line;
}
var prefixContent = s.slice(0, s.indexOf('$'));
return results.result.content
.map(function (_a) {
var _b = __read(_a, 2), from = _b[0], to = _b[1];
return results.content.slice(from, to);
})
.map(function (line) { return appendPrefix(prefixContent, line); })
.join('\n');
}, desc: 'extract found selections'
},
];
3 years ago
var TextExpander = /** @class */ (function (_super) {
__extends(TextExpander, _super);
function TextExpander(app, plugin) {
var _this = _super.call(this, app, plugin) || this;
_this.config = {
autoExpand: false,
defaultTemplate: '- $link',
3 years ago
delay: 300,
3 years ago
excludeCurrent: true,
3 years ago
lineEnding: '<-->'
3 years ago
};
3 years ago
_this.seqs = sequences;
3 years ago
_this.search = _this.search.bind(_this);
_this.initExpander = _this.initExpander.bind(_this);
_this.reformatLinks = _this.reformatLinks.bind(_this);
return _this;
}
TextExpander.prototype.getFrontMatter = function (s, r) {
var _a = this.app.metadataCache.getCache(r.path).frontmatter, frontmatter = _a === void 0 ? null : _a;
if (frontmatter) {
return frontmatter[s.split(':')[1]] || '';
}
return '';
};
TextExpander.prototype.reformatLinks = function (links, mapFunc) {
3 years ago
var _a, _b, _c, _d;
3 years ago
if (mapFunc === void 0) { mapFunc = function (s) { return '[[' + s + ']]'; }; }
var currentView = this.app.workspace.activeLeaf.view;
if (currentView instanceof obsidian.FileView) {
return (_b = (_a = links === null || links === void 0 ? void 0 : links.map(function (e) { return e.basename; }).filter(function (e) { return currentView.file.basename !== e; })) === null || _a === void 0 ? void 0 : _a.map(mapFunc)) === null || _b === void 0 ? void 0 : _b.join('\n');
}
3 years ago
return (_d = (_c = links === null || links === void 0 ? void 0 : links.map(function (e) { return e.basename; })) === null || _c === void 0 ? void 0 : _c.map(mapFunc)) === null || _d === void 0 ? void 0 : _d.join('\n');
3 years ago
};
TextExpander.prototype.search = function (s) {
// @ts-ignore
var globalSearchFn = this.app.internalPlugins.getPluginById('global-search').instance.openGlobalSearch.bind(this);
var search = function (query) { return globalSearchFn(query); };
var leftSplitState = {
// @ts-ignore
collapsed: this.app.workspace.leftSplit.collapsed,
// @ts-ignore
3 years ago
tab: this.getSearchTabIndex()
3 years ago
};
search(s);
if (leftSplitState.collapsed) {
// @ts-ignore
this.app.workspace.leftSplit.collapse();
}
// @ts-ignore
if (leftSplitState.tab !== this.app.workspace.leftSplit.children[0].currentTab) {
// @ts-ignore
this.app.workspace.leftSplit.children[0].selectTabIndex(leftSplitState.tab);
}
};
3 years ago
TextExpander.prototype.getSearchTabIndex = function () {
var leftTabs = this.app.workspace.leftSplit.children[0].children;
var searchTabId;
this.app.workspace.iterateAllLeaves(function (leaf) {
if (leaf.getViewState().type == "search") {
searchTabId = leaf.id;
}
});
return leftTabs.findIndex(function (item, index, array) {
if (item.id == searchTabId) {
return true;
}
});
};
3 years ago
TextExpander.prototype.getFoundAfterDelay = function () {
return __awaiter(this, void 0, void 0, function () {
var searchLeaf, view;
var _this = this;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
searchLeaf = this.app.workspace.getLeavesOfType('search')[0];
return [4 /*yield*/, searchLeaf.open(searchLeaf.view)];
case 1:
view = _a.sent();
return [2 /*return*/, new Promise(function (resolve) {
setTimeout(function () {
// @ts-ignore
var results = view.dom.resultDomLookup;
return resolve(results);
}, _this.config.delay);
})];
}
});
});
};
TextExpander.prototype.startTemplateMode = function (query, lastLine) {
var _a;
return __awaiter(this, void 0, void 0, function () {
var currentView, currentFileName, templateContent, heading, footer, repeatableContent, searchResults, files, filterFiles, format, changed, result, viewBeforeReplace;
var _this = this;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
currentView = this.app.workspace.activeLeaf.view;
currentFileName = '';
templateContent = query.template.split('\n');
heading = templateContent.filter(function (e) { return e[0] === '^'; }).map(function (s) { return s.slice(1); });
footer = templateContent.filter(function (e) { return e[0] === '>'; }).map(function (s) { return s.slice(1); });
repeatableContent = templateContent.filter(function (e) { return e[0] !== '^' && e[0] !== '>'; }).filter(function (e) { return e; }).length === 0
? [this.config.defaultTemplate]
: templateContent.filter(function (e) { return e[0] !== '^' && e[0] !== '>'; }).filter(function (e) { return e; });
if (currentView instanceof obsidian.FileView) {
currentFileName = currentView.file.basename;
}
return [4 /*yield*/, this.getFoundAfterDelay()];
case 1:
searchResults = _b.sent();
files = Array.from(searchResults.keys());
filterFiles = this.config.excludeCurrent
? files.filter(function (file) { return file.basename !== currentFileName; })
: files;
format = function (r, template, index) { return __awaiter(_this, void 0, void 0, function () {
var fileContent, _a;
3 years ago
var _this = this;
3 years ago
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
if (!(new RegExp(this.seqs.filter(function (e) { return e.readContent; }).map(function (e) { return e.name; }).join('|')).test(template))) return [3 /*break*/, 2];
return [4 /*yield*/, this.app.vault.cachedRead(r)];
case 1:
_a = _b.sent();
return [3 /*break*/, 3];
case 2:
_a = '';
_b.label = 3;
case 3:
fileContent = _a;
return [2 /*return*/, this.seqs.reduce(function (acc, seq) {
3 years ago
return acc.replace(new RegExp(seq.name, 'gu'), function (replace) { return seq.format(_this, replace, fileContent, r, searchResults.get(r), index); });
3 years ago
}, template)];
}
});
}); };
return [4 /*yield*/, Promise.all(filterFiles
.map(function (file, i) { return __awaiter(_this, void 0, void 0, function () {
var result;
var _this = this;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, Promise.all(repeatableContent.map(function (s) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, format(file, s, i)];
case 1: return [2 /*return*/, _a.sent()];
}
}); }); }))];
case 1:
result = _a.sent();
return [2 /*return*/, result.join('\n')];
}
});
}); }))];
case 2:
changed = _b.sent();
result = [
' ',
heading.join('\n'),
changed.join('\n'),
footer.join('\n'),
' ',
this.config.lineEnding
].filter(function (e) { return e; }).join('\n');
viewBeforeReplace = this.app.workspace.activeLeaf.view;
if (viewBeforeReplace instanceof obsidian.MarkdownView) {
if (viewBeforeReplace.file.basename !== currentFileName) {
return [2 /*return*/];
}
}
else {
return [2 /*return*/];
}
this.cm.replaceRange(result, { line: query.end + 1, ch: 0 }, { line: lastLine, ch: ((_a = this.cm.getLine(lastLine)) === null || _a === void 0 ? void 0 : _a.length) || 0 });
return [2 /*return*/, Promise.resolve()];
}
});
});
};
TextExpander.prototype.runQuery = function (query, content) {
var _a;
return __awaiter(this, void 0, void 0, function () {
3 years ago
var lineEnding, lastLine, newContent;
3 years ago
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
3 years ago
lineEnding = this.config.lineEnding;
3 years ago
if (!query) {
new Notification('Expand query not found');
return [2 /*return*/, Promise.resolve()];
}
lastLine = getLastLineToReplace(content, query, this.config.lineEnding);
3 years ago
this.cm.replaceRange('\n' + lineEnding, { line: query.end + 1, ch: 0 }, { line: lastLine, ch: ((_a = this.cm.getLine(lastLine)) === null || _a === void 0 ? void 0 : _a.length) || 0 });
3 years ago
newContent = formatContent(this.cm.getValue());
this.search(query.query);
return [4 /*yield*/, this.startTemplateMode(query, getLastLineToReplace(newContent, query, this.config.lineEnding))];
case 1: return [2 /*return*/, _b.sent()];
}
});
});
};
TextExpander.prototype.initExpander = function (all) {
var _this = this;
if (all === void 0) { all = false; }
var currentView = this.app.workspace.activeLeaf.view;
if (!(currentView instanceof obsidian.MarkdownView)) {
return;
}
var cmDoc = this.cm = currentView.sourceMode.cmEditor;
var curNum = cmDoc.getCursor().line;
var content = cmDoc.getValue();
var formatted = formatContent(content);
var findQueries = getAllExpandersQuery(formatted);
var closestQuery = getClosestQuery(findQueries, curNum);
if (all) {
findQueries.reduce(function (promise, query, i) {
return promise.then(function () {
var newContent = formatContent(cmDoc.getValue());
var updatedQueries = getAllExpandersQuery(newContent);
return _this.runQuery(updatedQueries[i], newContent);
});
}, Promise.resolve());
}
else {
this.runQuery(closestQuery, formatted);
}
};
TextExpander.prototype.onload = function () {
return __awaiter(this, void 0, void 0, function () {
var data;
var _this = this;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
this.addSettingTab(new SettingTab(this.app, this));
this.addCommand({
id: 'editor-expand',
name: 'expand',
callback: this.initExpander,
hotkeys: []
});
this.addCommand({
id: 'editor-expand-all',
name: 'expand all',
callback: function () { return _this.initExpander(true); },
hotkeys: []
});
this.app.workspace.on('file-open', function () { return __awaiter(_this, void 0, void 0, function () {
var activeLeaf, activeView, isAllowedView;
return __generator(this, function (_a) {
if (!this.config.autoExpand) {
return [2 /*return*/];
}
activeLeaf = this.app.workspace.activeLeaf;
if (!activeLeaf) {
return [2 /*return*/];
}
activeView = activeLeaf.view;
isAllowedView = activeView instanceof obsidian.MarkdownView;
if (!isAllowedView) {
return [2 /*return*/];
}
this.initExpander(true);
return [2 /*return*/];
});
}); });
return [4 /*yield*/, this.loadData()];
case 1:
data = _a.sent();
if (data) {
this.config = __assign(__assign({}, this.config), data);
}
return [2 /*return*/];
}
});
});
};
TextExpander.prototype.onunload = function () {
console.log('unloading plugin');
};
TextExpander.prototype.saveSettings = function () {
this.saveData(this.config);
};
return TextExpander;
}(obsidian.Plugin));
var SettingTab = /** @class */ (function (_super) {
__extends(SettingTab, _super);
function SettingTab(app, plugin) {
var _this = _super.call(this, app, plugin) || this;
_this.app = app;
_this.plugin = plugin;
return _this;
}
SettingTab.prototype.display = function () {
var _this = this;
var containerEl = this.containerEl;
containerEl.empty();
containerEl.createEl('h2', { text: 'Settings for Text Expander' });
new obsidian.Setting(containerEl)
.setName('Auto Expand')
.setDesc('Expand all queries in a file once you open it')
.addToggle(function (toggle) {
toggle
.setValue(_this.plugin.config.autoExpand)
.onChange(function (value) {
_this.plugin.config.autoExpand = value;
_this.plugin.saveSettings();
});
});
new obsidian.Setting(containerEl)
.setName('Delay')
.setDesc('Text expander don\' wait until search completed. It waits for a delay and paste result after that.')
.addSlider(function (slider) {
3 years ago
slider.setLimits(100, 10000, 100);
3 years ago
slider.setValue(_this.plugin.config.delay);
slider.onChange(function (value) {
_this.plugin.config.delay = value;
_this.plugin.saveSettings();
});
slider.setDynamicTooltip();
});
new obsidian.Setting(containerEl)
.setName('Line ending')
.setDesc('You can specify the text which will appear at the bottom of the generated text.')
.addText(function (text) {
text.setValue(_this.plugin.config.lineEnding)
.onChange(function (val) {
_this.plugin.config.lineEnding = val;
_this.plugin.saveSettings();
});
});
new obsidian.Setting(containerEl)
.setName('Default template')
.setDesc('You can specify default template')
.addText(function (text) {
text.setValue(_this.plugin.config.defaultTemplate)
.onChange(function (val) {
_this.plugin.config.defaultTemplate = val;
_this.plugin.saveSettings();
});
});
new obsidian.Setting(containerEl)
.setName('Exclude current file')
.setDesc('You can specify should text expander exclude results from current file or not')
.addToggle(function (toggle) {
toggle
.setValue(_this.plugin.config.excludeCurrent)
.onChange(function (value) {
_this.plugin.config.excludeCurrent = value;
_this.plugin.saveSettings();
});
});
new obsidian.Setting(containerEl)
.setName('Sequences')
.setDesc('REGEXP - DESCRIPTION')
.setDesc((function () {
var fragment = new DocumentFragment();
var div = fragment.createEl('div');
_this.plugin.seqs
.map(function (e) { return e.name + ' - ' + (e.desc || ''); })
.map(function (e) {
var el = fragment.createEl('div');
el.setText(e);
el.setAttribute('style', "\n border-bottom: 1px solid rgba(255, 255, 255, 0.1);\n margin-bottom: 0.5rem;\n padding-bottom: 0.5rem;\n ");
return el;
}).forEach(function (el) {
div.appendChild(el);
});
fragment.appendChild(div);
return fragment;
})());
};
return SettingTab;
}(obsidian.PluginSettingTab));
module.exports = TextExpander;
3 years ago
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibWFpbi5qcyIsInNvdXJjZXMiOlsibm9kZV9tb2R1bGVzL3RzbGliL3RzbGliLmVzNi5qcyIsImhlbHBlcnMudHMiLCJzcmMvc2VxdWVuY2VzL3NlcXVlbmNlcy50cyIsInNyYy9tYWluLnRzIl0sInNvdXJjZXNDb250ZW50IjpbIi8qKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKipcclxuQ29weXJpZ2h0IChjKSBNaWNyb3NvZnQgQ29ycG9yYXRpb24uXHJcblxyXG5QZXJtaXNzaW9uIHRvIHVzZSwgY29weSwgbW9kaWZ5LCBhbmQvb3IgZGlzdHJpYnV0ZSB0aGlzIHNvZnR3YXJlIGZvciBhbnlcclxucHVycG9zZSB3aXRoIG9yIHdpdGhvdXQgZmVlIGlzIG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