Skip to content

Commit

Permalink
feat: successfully copied over basic editor from tiptap
Browse files Browse the repository at this point in the history
For now this code is very much a typescript reimplimentation of tiptap

Credit where credit is due [tiptap](https://github.com/scrumpy/tiptap) is a fantastic library.
  • Loading branch information
ifiokjr committed Feb 7, 2019
1 parent 1474c3f commit 077610b
Show file tree
Hide file tree
Showing 37 changed files with 1,100 additions and 145 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,7 @@ dependencies/**/yarn.lock
# Cypress
/e2e/**/videos
*.mp4


#
.vscode
32 changes: 32 additions & 0 deletions @remirror/core-extensions/src/extensions/history.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { CommandFunction, Extension } from '@remirror/core';
import { history, redo, undo } from 'prosemirror-history';

export class History extends Extension {
public name = 'history';

public keys() {
const isMac = typeof navigator !== 'undefined' ? /Mac/.test(navigator.platform) : false;
const keymap: Record<'Mod-z' | 'Shift-Mod-z' | 'Mod-y', CommandFunction> = {
'Mod-y': () => false,
'Mod-z': undo,
'Shift-Mod-z': redo,
};

if (!isMac) {
keymap['Mod-y'] = redo;
}

return keymap;
}

get plugins() {
return [history()];
}

public commands() {
return {
undo: () => undo,
redo: () => redo,
};
}
}
4 changes: 3 additions & 1 deletion @remirror/core-extensions/src/extensions/placeholder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ export interface PlaceholderOptions {
export interface PlaceholderPluginState extends Required<PlaceholderOptions> {}

export class Placeholder extends Extension<PlaceholderOptions> {
public readonly name = 'placeholder';
get name() {
return 'placeholder';
}

get defaultOptions() {
return {
Expand Down
4 changes: 3 additions & 1 deletion @remirror/core-extensions/src/marks/bold.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import {
} from '@remirror/core';

export class Bold extends MarkExtension {
public readonly name = 'bold';
get name() {
return 'bold';
}

get schema(): MarkExtensionSpec {
return {
Expand Down
38 changes: 38 additions & 0 deletions @remirror/core-extensions/src/marks/code.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import {
MarkExtension,
markInputRule,
markPasteRule,
SchemaMarkTypeParams,
toggleMark,
} from '@remirror/core';

export class Code<GOptions extends {} = {}> extends MarkExtension<GOptions> {
get name() {
return 'code';
}

get schema() {
return {
parseDOM: [{ tag: 'code' }],
toDOM: () => ['code', 0],
};
}

public keys({ type }: SchemaMarkTypeParams) {
return {
'Mod-`': toggleMark(type),
};
}

public commands({ type }: SchemaMarkTypeParams) {
return () => toggleMark(type);
}

public inputRules({ type }: SchemaMarkTypeParams) {
return [markInputRule(/(?:`)([^`]+)(?:`)$/, type)];
}

public pasteRules({ type }: SchemaMarkTypeParams) {
return [markPasteRule(/(?:`)([^`]+)(?:`)/g, type)];
}
}
38 changes: 38 additions & 0 deletions @remirror/core-extensions/src/marks/italic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import {
MarkExtension,
markInputRule,
markPasteRule,
SchemaMarkTypeParams,
toggleMark,
} from '@remirror/core';

export class Italic extends MarkExtension {
get name() {
return 'italic';
}

get schema() {
return {
parseDOM: [{ tag: 'i' }, { tag: 'em' }, { style: 'font-style=italic' }],
toDOM: () => ['em', 0],
};
}

public keys({ type }: SchemaMarkTypeParams) {
return {
'Mod-i': toggleMark(type),
};
}

public commands({ type }: SchemaMarkTypeParams) {
return () => toggleMark(type);
}

public inputRules({ type }: SchemaMarkTypeParams) {
return [markInputRule(/(?:^|[^*_])(?:\*|_)([^*_]+)(?:\*|_)$/, type)];
}

public pasteRules({ type }: SchemaMarkTypeParams) {
return [markPasteRule(/(?:^|[^*_])(?:\*|_)([^*_]+)(?:\*|_)/g, type)];
}
}
89 changes: 89 additions & 0 deletions @remirror/core-extensions/src/marks/link.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import {
Attrs,
Cast,
getMarkRange,
MarkExtension,
MarkExtensionSpec,
pasteRule,
removeMark,
SchemaMarkTypeParams,
updateMark,
} from '@remirror/core';
import { Plugin, TextSelection } from 'prosemirror-state';

export class Link extends MarkExtension {
get name() {
return 'link';
}

get schema(): MarkExtensionSpec {
return {
attrs: {
href: {
default: null,
},
},
inclusive: false,
parseDOM: [
{
tag: 'a[href]',
getAttrs: dom => ({
href: Cast<Element>(dom).getAttribute('href'),
}),
},
],
toDOM: node => [
'a',
{
...node.attrs,
rel: 'noopener noreferrer nofollow',
},
0,
],
};
}

public commands({ type }: SchemaMarkTypeParams) {
return (attrs?: Attrs) => {
if (attrs && attrs.href) {
return updateMark(type, attrs);
}

return removeMark(type);
};
}

public pasteRules({ type }: SchemaMarkTypeParams) {
return [
pasteRule(
/https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_+.~#?&//=]*)/g,
type,
url => ({ href: url as string }),
),
];
}

get plugins() {
return [
new Plugin({
props: {
handleClick(view, pos) {
const { schema, doc, tr } = view.state;
const range = getMarkRange(doc.resolve(pos), schema.marks.link);

if (!range) {
return false;
}

const $start = doc.resolve(range.from);
const $end = doc.resolve(range.to);
const transaction = tr.setSelection(new TextSelection($start, $end));

view.dispatch(transaction);
return true;
},
},
}),
];
}
}
53 changes: 53 additions & 0 deletions @remirror/core-extensions/src/marks/strike.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import {
MarkExtension,
MarkExtensionSpec,
markInputRule,
markPasteRule,
SchemaMarkTypeParams,
toggleMark,
} from '@remirror/core';

export class Strike extends MarkExtension {
get name() {
return 'strike';
}

get schema(): MarkExtensionSpec {
return {
parseDOM: [
{
tag: 's',
},
{
tag: 'del',
},
{
tag: 'strike',
},
{
style: 'text-decoration',
getAttrs: value => (value === 'line-through' ? {} : false),
},
],
toDOM: () => ['s', 0],
};
}

public keys({ type }: SchemaMarkTypeParams) {
return {
'Mod-d': toggleMark(type),
};
}

public commands({ type }: SchemaMarkTypeParams) {
return () => toggleMark(type);
}

public inputRules({ type }: SchemaMarkTypeParams) {
return [markInputRule(/~([^~]+)~$/, type)];
}

public pasteRules({ type }: SchemaMarkTypeParams) {
return [markPasteRule(/~([^~]+)~/g, type)];
}
}
32 changes: 32 additions & 0 deletions @remirror/core-extensions/src/marks/underline.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { MarkExtension, MarkExtensionSpec, SchemaMarkTypeParams, toggleMark } from '@remirror/core';

export class Underline extends MarkExtension {
get name() {
return 'underline';
}

get schema(): MarkExtensionSpec {
return {
parseDOM: [
{
tag: 'u',
},
{
style: 'text-decoration',
getAttrs: value => (value === 'underline' ? {} : false),
},
],
toDOM: () => ['u', 0],
};
}

public keys({ type }: SchemaMarkTypeParams) {
return {
'Mod-u': toggleMark(type),
};
}

public commands({ type }: SchemaMarkTypeParams) {
return () => toggleMark(type);
}
}
32 changes: 32 additions & 0 deletions @remirror/core-extensions/src/nodes/block-quote.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { NodeExtension, SchemaNodeTypeParams, toggleWrap, wrappingInputRule } from '@remirror/core';

export class Blockquote extends NodeExtension {
get name() {
return 'blockquote';
}

get schema() {
return {
content: 'block*',
group: 'block',
defining: true,
draggable: false,
parseDOM: [{ tag: 'blockquote' }],
toDOM: () => ['blockquote', 0],
};
}

public commands({ type }: SchemaNodeTypeParams) {
return () => toggleWrap(type);
}

public keys({ type }: SchemaNodeTypeParams) {
return {
'Ctrl->': toggleWrap(type),
};
}

public inputRules({ type }: SchemaNodeTypeParams) {
return [wrappingInputRule(/^\s*>\s$/, type)];
}
}
30 changes: 30 additions & 0 deletions @remirror/core-extensions/src/nodes/bullet-list.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { NodeExtension, SchemaNodeTypeParams, toggleList, wrappingInputRule } from '@remirror/core';

export class Bullet extends NodeExtension {
get name() {
return 'bullet_list';
}

get schema() {
return {
content: 'list_item+',
group: 'block',
parseDOM: [{ tag: 'ul' }],
toDOM: () => ['ul', 0],
};
}

public commands({ type, schema }: SchemaNodeTypeParams) {
return () => toggleList(type, schema.nodes.list_item);
}

public keys({ type, schema }: SchemaNodeTypeParams) {
return {
'Shift-Ctrl-8': toggleList(type, schema.nodes.list_item),
};
}

public inputRules({ type }: SchemaNodeTypeParams) {
return [wrappingInputRule(/^\s*([-+*])\s$/, type)];
}
}

0 comments on commit 077610b

Please sign in to comment.