Improve link search handling
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
e1254ba3b4
commit
2b43fd9ea9
1457
editor/package-lock.json
generated
1457
editor/package-lock.json
generated
File diff suppressed because it is too large
Load Diff
|
@ -1,9 +1,12 @@
|
|||
{
|
||||
"devDependencies": {
|
||||
"clean-webpack-plugin": "^3.0.0",
|
||||
"esm": "^3.2.25",
|
||||
"html-webpack-plugin": "^3.2.0",
|
||||
"mocha": "^7.2.0",
|
||||
"mocha-webpack": "^1.1.0",
|
||||
"scss-loader": "0.0.1",
|
||||
"webpack": "^4.39.2",
|
||||
"webpack": "^4.43.0",
|
||||
"webpack-cli": "^3.3.7",
|
||||
"webpack-dev-server": "^3.11.0"
|
||||
},
|
||||
|
|
|
@ -4,6 +4,7 @@ import qs from 'querystring'
|
|||
import $ from 'jquery';
|
||||
import search from './search';
|
||||
import createPageSearch from './fuse';
|
||||
import util from './util';
|
||||
import Mustache from 'mustache';
|
||||
import 'jquery-contextmenu';
|
||||
import getCaretCoordinates from './caret-position'
|
||||
|
@ -261,19 +262,10 @@ if (holder) {
|
|||
let value = input.value
|
||||
let end = input.selectionEnd
|
||||
|
||||
let insideLink = true
|
||||
let [start, insideLink] = util.cursorInsideLink(value, end)
|
||||
console.log(value, end, start, insideLink)
|
||||
let insideSearch = false
|
||||
|
||||
let start = value.lastIndexOf("[[", end)
|
||||
if (start < 0) {
|
||||
insideLink = false
|
||||
}
|
||||
|
||||
let linkEnd = value.lastIndexOf("]]", end - 1)
|
||||
if (insideLink && start < linkEnd) {
|
||||
insideLink = false
|
||||
}
|
||||
|
||||
if (!insideLink) {
|
||||
start = value.lastIndexOf("/", end)
|
||||
insideSearch = start >= 0
|
||||
|
@ -287,6 +279,8 @@ if (holder) {
|
|||
let query = value.substring(start + 2, end);
|
||||
showSearchResults(query => titleSearch.search(query), query, input, value, 'link');
|
||||
return true
|
||||
} else {
|
||||
$('#link-complete').fadeOut();
|
||||
}
|
||||
})
|
||||
})
|
||||
|
|
21
editor/src/util.js
Normal file
21
editor/src/util.js
Normal file
|
@ -0,0 +1,21 @@
|
|||
function cursorInsideLink(value, pos) {
|
||||
let start = value.lastIndexOf("[[", pos)
|
||||
|
||||
if (start < 0) {
|
||||
return [null, false];
|
||||
}
|
||||
|
||||
if (start === pos) {
|
||||
return [null, false];
|
||||
}
|
||||
if (start === pos-1) {
|
||||
return [null, false];
|
||||
}
|
||||
|
||||
let linkEnd = value.lastIndexOf("]]", pos-1)
|
||||
return [start+2, start >= linkEnd]
|
||||
}
|
||||
|
||||
export default {
|
||||
cursorInsideLink
|
||||
}
|
46
editor/test/link.test.js
Normal file
46
editor/test/link.test.js
Normal file
|
@ -0,0 +1,46 @@
|
|||
// let assert = require('assert');
|
||||
// let util = require('../src/util')
|
||||
|
||||
import assert from 'assert'
|
||||
import util from '../src/util'
|
||||
|
||||
describe('Link', function () {
|
||||
describe('#cursorInsideLink', function () {
|
||||
it('should return true when inside link', function () {
|
||||
assert.deepStrictEqual(util.cursorInsideLink("[[test]]", 2), [2, true]);
|
||||
})
|
||||
it('should return true when inside link position 1', function () {
|
||||
assert.deepStrictEqual(util.cursorInsideLink("[[test]]", 3), [2,true]);
|
||||
})
|
||||
it('should return true when inside link position 2', function () {
|
||||
assert.deepStrictEqual(util.cursorInsideLink("[[test]]", 4), [2,true]);
|
||||
})
|
||||
it('should return true when inside link position 3', function () {
|
||||
assert.deepStrictEqual(util.cursorInsideLink("[[test]]", 5), [2,true]);
|
||||
})
|
||||
it('should return true when inside link position 4', function () {
|
||||
assert.deepStrictEqual(util.cursorInsideLink("[[test]]", 6), [2,true]);
|
||||
})
|
||||
it('should return false on empty string', function () {
|
||||
assert.strictEqual(util.cursorInsideLink("", 0)[1], false);
|
||||
});
|
||||
it('should return false before open link inside whitespace', function () {
|
||||
assert.strictEqual(util.cursorInsideLink(" [[", 0)[1], false);
|
||||
})
|
||||
it('should return false before one space and open link', function () {
|
||||
assert.strictEqual(util.cursorInsideLink(" [[", 0)[1], false);
|
||||
})
|
||||
it('should return false when before closed link', function () {
|
||||
assert.strictEqual(util.cursorInsideLink("[[test]]", 0)[1], false);
|
||||
})
|
||||
it('should return false when on closed link start tag', function () {
|
||||
assert.strictEqual(util.cursorInsideLink("[[test]]", 1)[1], false);
|
||||
})
|
||||
it('should return false when on link end tag', function () {
|
||||
assert.strictEqual(util.cursorInsideLink("[[test]]", 7)[1], false);
|
||||
})
|
||||
it('should return false when after link end tag', function () {
|
||||
assert.strictEqual(util.cursorInsideLink("[[test]]", 7)[1], false);
|
||||
})
|
||||
})
|
||||
})
|
Loading…
Reference in New Issue
Block a user