Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added inCode flag #8

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 14 additions & 3 deletions src/inputrules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export class InputRule {

/// @internal
undoable: boolean
inCode?: 'allowed' | 'only';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you make this boolean | "only", so people can also explicitly provide false?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.


// :: (RegExp, union<string, (state: EditorState, match: [string], start: number, end: number) → ?Transaction>)
/// Create an input rule. The rule applies when the user typed
Expand All @@ -35,12 +36,17 @@ export class InputRule {
/// When set to false,
/// [`undoInputRule`](#inputrules.undoInputRule) doesn't work on
/// this rule.
undoable?: boolean
undoable?: 'boolean',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this should be a string.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, you're right, don't know how I changed that.

/// When set to 'allowed', the rule applies to both code and non-code blocks
/// When set to 'only', the rule only applies to code blocks
/// otherwise the rule only applies to non-code blocks
inCode?: 'allowed' | 'only';
} = {}
) {
this.match = match
this.handler = typeof handler == "string" ? stringHandler(handler) : handler
this.undoable = options.undoable !== false
this.inCode = options.inCode
}
}

Expand Down Expand Up @@ -101,11 +107,16 @@ export function inputRules({rules}: {rules: readonly InputRule[]}) {
function run(view: EditorView, from: number, to: number, text: string, rules: readonly InputRule[], plugin: Plugin) {
if (view.composing) return false
let state = view.state, $from = state.doc.resolve(from)
if ($from.parent.type.spec.code) return false
let textBefore = $from.parent.textBetween(Math.max(0, $from.parentOffset - MAX_MATCH), $from.parentOffset,
null, "\ufffc") + text
for (let i = 0; i < rules.length; i++) {
let rule = rules[i], match = rule.match.exec(textBefore)
let rule = rules[i];
if ($from.parent.type.spec.code) {
if (!rule.inCode) continue; // skip rules that don't target code blocks
} else if (rule.inCode === 'only') {
continue;// skip inCode rules for non code blocks
}
let match = rule.match.exec(textBefore)
let tr = match && rule.handler(state, match, from - (match[0].length - text.length), to)
if (!tr) continue
if (rule.undoable) tr.setMeta(plugin, {transform: tr, from, to, text})
Expand Down