-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c80f89c
commit f0ba825
Showing
12 changed files
with
245 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.strikethrough | ||
text-decoration: line-through | ||
opacity: 0.5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
import * as _ from 'lodash'; | ||
|
||
import './index.sass'; | ||
|
||
import { hideBorderAndModify, RegexTokenizerModifier } from '../../assets/js/utils/token_unfolder'; | ||
import { registerPlugin } from '../../assets/js/plugins'; | ||
import { matchWordRegex } from '../../assets/js/utils'; | ||
import { Row } from '../../assets/js/types'; | ||
import Session from '../../assets/js/session'; | ||
|
||
const strikethroughClass = 'strikethrough'; | ||
|
||
export const pluginName = 'Todo'; | ||
|
||
registerPlugin<void>( | ||
{ | ||
name: pluginName, | ||
author: 'Jeff Wu', | ||
description: `Lets you strike out bullets`, | ||
}, | ||
function(api) { | ||
api.registerHook('session', 'renderLineTokenHook', (tokenizer, hooksInfo) => { | ||
if (hooksInfo.has_cursor) { | ||
return tokenizer; | ||
} | ||
if (hooksInfo.has_highlight) { | ||
return tokenizer; | ||
} | ||
return tokenizer.then(RegexTokenizerModifier<React.ReactNode>( | ||
matchWordRegex('\\~\\~(\\n|.)+?\\~\\~'), | ||
hideBorderAndModify(2, 2, (char_info) => { char_info.renderOptions.classes[strikethroughClass] = true; }) | ||
)); | ||
}); | ||
|
||
async function isStruckThrough(session: Session, row: Row) { | ||
const text = await session.document.getText(row); | ||
return (text.slice(0, 2) === '~~') && (text.slice(-2) === '~~'); | ||
} | ||
|
||
async function addStrikeThrough(session: Session, row: Row) { | ||
await session.addChars(row, -1, ['~', '~']); | ||
await session.addChars(row, 0, ['~', '~']); | ||
} | ||
|
||
async function removeStrikeThrough(session: Session, row: Row) { | ||
await session.delChars(row, -2, 2); | ||
await session.delChars(row, 0, 2); | ||
} | ||
|
||
api.registerAction( | ||
'toggle-strikethrough', | ||
'Toggle strikethrough for a row', | ||
async function({ session }) { | ||
if (await isStruckThrough(session, session.cursor.row)) { | ||
await removeStrikeThrough(session, session.cursor.row); | ||
} else { | ||
await addStrikeThrough(session, session.cursor.row); | ||
} | ||
}, | ||
); | ||
|
||
// TODO: this should maybe strikethrough children, since UI suggests it? | ||
api.registerAction( | ||
'visual-line-toggle-strikethrough', | ||
'Toggle strikethrough for rows', | ||
async function({ session, visual_line }) { | ||
if (visual_line == null) { | ||
throw new Error('Visual_line mode arguments missing'); | ||
} | ||
|
||
const is_struckthrough = await Promise.all( | ||
visual_line.selected.map(async (path) => { | ||
return await isStruckThrough(session, path.row); | ||
}) | ||
); | ||
if (_.every(is_struckthrough)) { | ||
await Promise.all( | ||
visual_line.selected.map(async (path) => { | ||
await removeStrikeThrough(session, path.row); | ||
}) | ||
); | ||
} else { | ||
await Promise.all( | ||
visual_line.selected.map(async (path, i) => { | ||
if (!is_struckthrough[i]) { | ||
await addStrikeThrough(session, path.row); | ||
} | ||
}) | ||
); | ||
} | ||
await session.setMode('NORMAL'); | ||
}, | ||
); | ||
|
||
api.registerDefaultMappings( | ||
'NORMAL', | ||
{ | ||
'toggle-strikethrough': [['ctrl+enter']], | ||
}, | ||
); | ||
|
||
api.registerDefaultMappings( | ||
'INSERT', | ||
{ | ||
'toggle-strikethrough': [['ctrl+enter', 'meta+enter']], | ||
}, | ||
); | ||
|
||
api.registerDefaultMappings( | ||
'VISUAL_LINE', | ||
{ | ||
'visual-line-toggle-strikethrough': [['ctrl+enter']], | ||
}, | ||
); | ||
|
||
// TODO for workflowy mode | ||
// NOTE: in workflowy, this also crosses out children | ||
// 'toggle-strikethrough': [['meta+enter']], | ||
}, | ||
(api => api.deregisterAll()), | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
--require ts-node/register | ||
--require ignore-styles | ||
--watch-extensions tsx,ts | ||
--timeout 60000 | ||
test/tests/*.ts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* globals describe, it */ | ||
import TestCase from '../testcase'; | ||
import * as Todo from '../../src/plugins/todo'; | ||
import '../../src/assets/js/plugins'; | ||
|
||
const toggleStrikethroughKey = 'ctrl+enter'; | ||
|
||
describe('todo', function() { | ||
it('works in basic case', async function() { | ||
let t = new TestCase([ | ||
'a line', | ||
'another line', | ||
], {plugins: [Todo.pluginName]}); | ||
t.sendKey(toggleStrikethroughKey); | ||
t.expect([ | ||
'~~a line~~', | ||
'another line', | ||
]); | ||
|
||
t.sendKey(toggleStrikethroughKey); | ||
t.expect([ | ||
'a line', | ||
'another line', | ||
]); | ||
|
||
t.sendKey('u'); | ||
t.expect([ | ||
'~~a line~~', | ||
'another line', | ||
]); | ||
|
||
t.sendKey('u'); | ||
t.expect([ | ||
'a line', | ||
'another line', | ||
]); | ||
await t.done(); | ||
}); | ||
|
||
it('works in visual line', async function() { | ||
let t = new TestCase([ | ||
'a line', | ||
'~~another line~~', | ||
], {plugins: [Todo.pluginName]}); | ||
t.sendKeys('Vj'); | ||
t.sendKey(toggleStrikethroughKey); | ||
t.expect([ | ||
'~~a line~~', | ||
'~~another line~~', | ||
]); | ||
|
||
t.sendKeys('Vk'); | ||
t.sendKey(toggleStrikethroughKey); | ||
t.expect([ | ||
'a line', | ||
'another line', | ||
]); | ||
|
||
t.sendKeys('Vj'); | ||
t.sendKey(toggleStrikethroughKey); | ||
t.expect([ | ||
'~~a line~~', | ||
'~~another line~~', | ||
]); | ||
|
||
t.sendKey('u'); | ||
t.expect([ | ||
'a line', | ||
'another line', | ||
]); | ||
|
||
t.sendKey('u'); | ||
t.expect([ | ||
'~~a line~~', | ||
'~~another line~~', | ||
]); | ||
|
||
t.sendKey('u'); | ||
t.expect([ | ||
'a line', | ||
'~~another line~~', | ||
]); | ||
await t.done(); | ||
}); | ||
}); |