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

Adding types to Linter and making the structure a bit easier #1492

Merged
merged 6 commits into from
Jul 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 23 additions & 12 deletions docs/src/demos/Experiments/Linter/extension/Linter.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
// @ts-nocheck
import { Extension } from '@tiptap/core'
import { Decoration, DecorationSet } from 'prosemirror-view'
import { Plugin, PluginKey, TextSelection } from 'prosemirror-state'
import { Node as ProsemirrorNode } from 'prosemirror-model'
import LinterPlugin, { Result as Issue } from './LinterPlugin'

function renderIcon(issue) {
const icon = document.createElement('div')
interface IconDivElement extends HTMLDivElement {
issue?: Issue
}

function renderIcon(issue: Issue) {
const icon: IconDivElement = document.createElement('div')

icon.className = 'lint-icon'
icon.title = issue.message
Expand All @@ -13,11 +18,11 @@ function renderIcon(issue) {
return icon
}

function runAllLinterPlugins(doc, plugins) {
function runAllLinterPlugins(doc: ProsemirrorNode, plugins: Array<typeof LinterPlugin>) {
const decorations: [any?] = []

const results = plugins.map(LinterPlugin => {
return new LinterPlugin(doc).scan().getResults()
const results = plugins.map(RegisteredLinterPlugin => {
return new RegisteredLinterPlugin(doc).scan().getResults()
}).flat()

results.forEach(issue => {
Expand All @@ -31,7 +36,7 @@ function runAllLinterPlugins(doc, plugins) {
}

export interface LinterOptions {
plugins: [any],
plugins: Array<typeof LinterPlugin>,
}

export const Linter = Extension.create({
Expand Down Expand Up @@ -62,8 +67,9 @@ export const Linter = Extension.create({
return this.getState(state)
},
handleClick(view, _, event) {
if (/lint-icon/.test(event.target.className)) {
const { from, to } = event.target.issue
const target = (event.target as IconDivElement)
if (/lint-icon/.test(target.className) && target.issue) {
const { from, to } = target.issue

view.dispatch(
view.state.tr
Expand All @@ -73,17 +79,22 @@ export const Linter = Extension.create({

return true
}

return false
},
handleDoubleClick(view, _, event) {
if (/lint-icon/.test(event.target.className)) {
const prob = event.target.issue
const target = (event.target as IconDivElement)
if (/lint-icon/.test((event.target as HTMLElement).className) && target.issue) {
const prob = target.issue

if (prob.fix) {
prob.fix(view)
prob.fix(view, prob)
view.focus()
return true
}
}

return false
},
},
}),
Expand Down
14 changes: 10 additions & 4 deletions docs/src/demos/Experiments/Linter/extension/LinterPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
interface Result {
import { Node as ProsemirrorNode } from 'prosemirror-model'

export interface Result {
message: string,
from: number,
to: number,
fix?: null
fix?: Function
}

export default class LinterPlugin {
protected doc

private results: Array<Result> = []

constructor(doc: any) {
constructor(doc: ProsemirrorNode) {
this.doc = doc
}

record(message: string, from: number, to: number, fix?: null) {
record(message: string, from: number, to: number, fix?: Function) {
this.results.push({
message,
from,
Expand All @@ -23,6 +25,10 @@ export default class LinterPlugin {
})
}

scan() {
return this
}

getResults() {
return this.results
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
// @ts-nocheck
import LinterPlugin from '../LinterPlugin'

export class BadWords extends LinterPlugin {

public regex = /\b(obviously|clearly|evidently|simply)\b/ig

scan() {
this.doc.descendants((node: any, position: any) => {
this.doc.descendants((node: any, position: number) => {
if (!node.isText) {
return
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
// @ts-nocheck
import LinterPlugin from '../LinterPlugin'
import { EditorView } from 'prosemirror-view'
import LinterPlugin, { Result as Issue } from '../LinterPlugin'

export class HeadingLevel extends LinterPlugin {
fixHeader(level) {
return function ({ state, dispatch }) {
dispatch(state.tr.setNodeMarkup(this.from - 1, null, { level }))
fixHeader(level: number) {
return function ({ state, dispatch }: EditorView, issue: Issue) {
dispatch(state.tr.setNodeMarkup(issue.from - 1, undefined, { level }))
}
}

scan() {
let lastHeadLevel = null
let lastHeadLevel: number | null = null

this.doc.descendants((node, position) => {
if (node.type.name === 'heading') {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
// @ts-nocheck
import LinterPlugin from '../LinterPlugin'
import { EditorView } from 'prosemirror-view'
import LinterPlugin, { Result as Issue } from '../LinterPlugin'

export class Punctuation extends LinterPlugin {
public regex = / ([,.!?:]) ?/g

fix(replacement: any) {
return function ({ state, dispatch }) {
return function ({ state, dispatch }: EditorView, issue: Issue) {
dispatch(
state.tr.replaceWith(
this.from, this.to,
issue.from, issue.to,
state.schema.text(replacement),
),
)
Expand All @@ -17,9 +17,9 @@ export class Punctuation extends LinterPlugin {

scan() {
this.doc.descendants((node, position) => {
if (!node.isText) {
return
}
if (!node.isText) return

if (!node.text) return

const matches = this.regex.exec(node.text)

Expand Down
4 changes: 3 additions & 1 deletion docs/src/docPages/experiments/linter.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Linter

⚠️ Experiment
⚠️ Experiment, currently not supported or maintained

Linter can be used to check the content as per your wish and highlight it to the user. Linter extension can have multiple plugins for each task you want to achieve.

<demo name="Experiments/Linter" />