1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-09-27 22:58:19 +00:00

Add a custom collator that matches JS ordering

This commit is contained in:
Jeremy Ruston 2023-07-07 08:20:57 +01:00
parent f3bc32a2e9
commit d2e21ddd3c

View File

@ -17,24 +17,18 @@ $tw.SqlFunctions = function(options) {
// Setup custom collation to precisely match existing sort orders // Setup custom collation to precisely match existing sort orders
// Create field with `title TEXT NOT NULL COLLATE custom_collation` // Create field with `title TEXT NOT NULL COLLATE custom_collation`
// Use it like `... order by shadow collate custom_collation` // Use it like `... order by shadow collate custom_collation`
// function customCollation(ptr,lenA,a,lenB,b) { function customCollation(ptr,lenA,a,lenB,b) {
// var jsA = $tw.sqlite3.wasm.cstrToJs(a).slice(0,lenA), var jsA = $tw.sqlite3.wasm.cstrToJs(a).slice(0,lenA),
// jsB = $tw.sqlite3.wasm.cstrToJs(b).slice(0,lenB); jsB = $tw.sqlite3.wasm.cstrToJs(b).slice(0,lenB);
// if(jsA < jsB) { return jsA.localeCompare(jsB);
// return -1; }
// } else if (jsA > jsB) { var SQLITE_UTF8 = 1; /* IMP: R-37514-35566 */
// return +1; var SQLITE_UTF16LE = 2; /* IMP: R-03371-37637 */
// } else { var SQLITE_UTF16BE = 3; /* IMP: R-51971-34154 */
// return 0; var SQLITE_UTF16 = 4; /* Use native byte order */
// } var SQLITE_ANY = 5; /* Deprecated */
// } var SQLITE_UTF16_ALIGNED = 8; /* sqlite3_create_collation only */
// var SQLITE_UTF8 = 1; /* IMP: R-37514-35566 */ var collationResult = $tw.sqlite3.capi.sqlite3_create_collation_v2(this.db.pointer,"custom_collation",SQLITE_UTF8,this,customCollation,0);
// var SQLITE_UTF16LE = 2; /* IMP: R-03371-37637 */
// var SQLITE_UTF16BE = 3; /* IMP: R-51971-34154 */
// var SQLITE_UTF16 = 4; /* Use native byte order */
// var SQLITE_ANY = 5; /* Deprecated */
// var SQLITE_UTF16_ALIGNED = 8; /* sqlite3_create_collation only */
// var collationResult = $tw.sqlite3.capi.sqlite3_create_collation_v2(this.db.pointer,"custom_collation",SQLITE_UTF8,this,customCollation,0);
/* /*
Create tables and indexes Create tables and indexes
*/ */
@ -42,7 +36,7 @@ $tw.SqlFunctions = function(options) {
sql: ` sql: `
DROP TABLE IF EXISTS tiddlers; DROP TABLE IF EXISTS tiddlers;
CREATE TABLE tiddlers ( CREATE TABLE tiddlers (
title TEXT NOT NULL, title TEXT NOT NULL COLLATE custom_collation,
shadow INTEGER NOT NULL CHECK (shadow = 0 OR shadow = 1), -- 0=real tiddler, 1=shadow tiddler shadow INTEGER NOT NULL CHECK (shadow = 0 OR shadow = 1), -- 0=real tiddler, 1=shadow tiddler
shadowsource TEXT, shadowsource TEXT,
meta TEXT NOT NULL, meta TEXT NOT NULL,
@ -121,7 +115,7 @@ $tw.SqlFunctions = function(options) {
return undefined; return undefined;
} }
}; };
var statementAllTitles = self.db.prepare("select title from tiddlers where shadow = 0 order by title"); var statementAllTitles = self.db.prepare("select title from tiddlers where shadow = 0 order by title collate custom_collation");
this.sqlAllTitles = function() { this.sqlAllTitles = function() {
let resultRows = []; let resultRows = [];
while(statementAllTitles.step()) { while(statementAllTitles.step()) {
@ -131,7 +125,7 @@ $tw.SqlFunctions = function(options) {
statementAllTitles.reset(); statementAllTitles.reset();
return resultRows; return resultRows;
}; };
var statementAllShadowTitles = self.db.prepare("select title from tiddlers where shadow = 1 order by title"); var statementAllShadowTitles = self.db.prepare("select title from tiddlers where shadow = 1 order by title collate custom_collation");
this.sqlAllShadowTitles = function() { this.sqlAllShadowTitles = function() {
let resultRows = []; let resultRows = [];
while(statementAllShadowTitles.step()) { while(statementAllShadowTitles.step()) {
@ -141,7 +135,7 @@ $tw.SqlFunctions = function(options) {
statementAllShadowTitles.reset(); statementAllShadowTitles.reset();
return resultRows; return resultRows;
}; };
var statementEachTiddler = self.db.prepare("select title, meta, text from tiddlers where shadow = 0 order by title"); var statementEachTiddler = self.db.prepare("select title, meta, text from tiddlers where shadow = 0 order by title collate custom_collation");
this.sqlEachTiddler = function(callback) { this.sqlEachTiddler = function(callback) {
while(statementEachTiddler.step()) { while(statementEachTiddler.step()) {
var row = statementEachTiddler.get({}), var row = statementEachTiddler.get({}),
@ -175,7 +169,7 @@ $tw.SqlFunctions = function(options) {
where t4.title = t3.title where t4.title = t3.title
and t4.shadow = 1 and t4.shadow = 1
) )
order by title; order by title collate custom_collation;
`); `);
this.sqlEachShadowTiddler = function(callback) { this.sqlEachShadowTiddler = function(callback) {
while(statementEachShadowTiddler.step()) { while(statementEachShadowTiddler.step()) {
@ -198,7 +192,7 @@ $tw.SqlFunctions = function(options) {
where t2.title = t1.title where t2.title = t1.title
and t2.shadow = 0 and t2.shadow = 0
) )
order by t1.title; order by t1.title collate custom_collation;
`); `);
this.sqlEachTiddlerPlusShadows = function(callback) { this.sqlEachTiddlerPlusShadows = function(callback) {
self.sqlEachTiddler(callback); self.sqlEachTiddler(callback);
@ -221,7 +215,7 @@ $tw.SqlFunctions = function(options) {
where t2.title = t1.title where t2.title = t1.title
and t2.shadow = 1 and t2.shadow = 1
) )
order by t1.title; order by t1.title collate custom_collation;
`); `);
this.sqlEachShadowPlusTiddlers = function(callback) { this.sqlEachShadowPlusTiddlers = function(callback) {
self.sqlEachShadowTiddler(callback); self.sqlEachShadowTiddler(callback);