postmerge

This commit is contained in:
tonyrewin 2022-10-21 14:07:50 +03:00
parent 9f572ca590
commit 1055c006fc
8 changed files with 46 additions and 41 deletions

View File

@ -1,5 +1,5 @@
import { EditorView } from 'prosemirror-view' import type { EditorView } from 'prosemirror-view'
import { EditorState } from 'prosemirror-state' import type { EditorState } from 'prosemirror-state'
import { useState } from '../store/context' import { useState } from '../store/context'
import { ProseMirror } from './ProseMirror' import { ProseMirror } from './ProseMirror'
import '../styles/Editor.scss' import '../styles/Editor.scss'
@ -10,7 +10,7 @@ export const Editor = () => {
const onReconfigure = (text: EditorState) => ctrl.setState({ text }) const onReconfigure = (text: EditorState) => ctrl.setState({ text })
const onChange = (text: EditorState) => ctrl.setState({ text, lastModified: new Date() }) const onChange = (text: EditorState) => ctrl.setState({ text, lastModified: new Date() })
// const editorCss = (config) => css`` // const editorCss = (config) => css``
const style = () => (store.error ? `display: none;` : store.markdown ? `white-space: pre-wrap;` : '') const style = () => (store.error ? `display: none;` : (store.markdown ? `white-space: pre-wrap;` : ''))
return ( return (
<ProseMirror <ProseMirror
className='editor col-md-6 shift-content' className='editor col-md-6 shift-content'

View File

@ -1,12 +1,13 @@
import { Config } from '../store/context' import type { JSX } from 'solid-js/jsx-runtime';
import type { Config } from '../store/context'
import '../styles/Layout.scss' import '../styles/Layout.scss'
export type Styled = { export type Styled = {
children: any; children: JSX.Element;
config?: Config; config?: Config;
'data-testid'?: string; 'data-testid'?: string;
onClick?: () => void; onClick?: () => void;
onMouseEnter?: (e: any) => void; onMouseEnter?: (e: MouseEvent) => void;
} }
export const Layout = (props: Styled) => { export const Layout = (props: Styled) => {

View File

@ -1,11 +1,11 @@
import { createEffect, untrack } from 'solid-js' import { createEffect, untrack } from 'solid-js'
import { Store, unwrap } from 'solid-js/store' import { Store, unwrap } from 'solid-js/store'
import { EditorState, Transaction } from 'prosemirror-state' import { EditorState, EditorStateConfig, Transaction } from 'prosemirror-state'
import { EditorView } from 'prosemirror-view' import { EditorView } from 'prosemirror-view'
import { Schema } from 'prosemirror-model' import { Schema } from 'prosemirror-model'
import { NodeViewFn, ProseMirrorExtension, ProseMirrorState } from '../prosemirror/helpers' import type { NodeViewFn, ProseMirrorExtension, ProseMirrorState } from '../prosemirror/helpers'
interface Props { interface ProseMirrorProps {
style?: string; style?: string;
className?: string; className?: string;
text?: Store<ProseMirrorState>; text?: Store<ProseMirrorState>;
@ -16,7 +16,7 @@ interface Props {
onChange: (s: EditorState) => void; onChange: (s: EditorState) => void;
} }
export const ProseMirror = (props: Props) => { export const ProseMirror = (props: ProseMirrorProps) => {
let editorRef: HTMLDivElement let editorRef: HTMLDivElement
const editorView = () => untrack(() => unwrap(props.editorView)) const editorView = () => untrack(() => unwrap(props.editorView))
@ -30,7 +30,7 @@ export const ProseMirror = (props: Props) => {
createEffect((payload: [EditorState, ProseMirrorExtension[]]) => { createEffect((payload: [EditorState, ProseMirrorExtension[]]) => {
const [prevText, prevExtensions] = payload const [prevText, prevExtensions] = payload
const text: EditorState = unwrap(props.text) const text = unwrap(props.text)
const extensions: ProseMirrorExtension[] = unwrap(props.extensions) const extensions: ProseMirrorExtension[] = unwrap(props.extensions)
if (!text || !extensions?.length) { if (!text || !extensions?.length) {
return [text, extensions] return [text, extensions]
@ -63,7 +63,7 @@ export const ProseMirror = (props: Props) => {
<div <div
style={props.style} style={props.style}
ref={editorRef} ref={editorRef}
className={props.className} class={props.className}
spell-check={false} spell-check={false}
/> />
) )
@ -101,7 +101,7 @@ const createEditorState = (
let editorState: EditorState let editorState: EditorState
if (reconfigure) { if (reconfigure) {
editorState = text.reconfigure({ schema, plugins }) editorState = text.reconfigure({ schema, plugins } as EditorStateConfig)
} else if (text instanceof EditorState) { } else if (text instanceof EditorState) {
editorState = EditorState.fromJSON({ schema, plugins }, text.toJSON()) editorState = EditorState.fromJSON({ schema, plugins }, text.toJSON())
} else if (text){ } else if (text){

View File

@ -1,7 +1,7 @@
import markdownit from 'markdown-it' import markdownit from 'markdown-it'
import { MarkdownSerializer, MarkdownParser, defaultMarkdownSerializer } from 'prosemirror-markdown' import { MarkdownSerializer, MarkdownParser, defaultMarkdownSerializer, MarkdownSerializerState } from 'prosemirror-markdown'
import { Node, Schema } from 'prosemirror-model' import type { Node, Schema } from 'prosemirror-model'
import { EditorState } from 'prosemirror-state' import type { EditorState } from 'prosemirror-state'
export const serialize = (state: EditorState) => { export const serialize = (state: EditorState) => {
let text = markdownSerializer.serialize(state.doc) let text = markdownSerializer.serialize(state.doc)
@ -12,14 +12,30 @@ export const serialize = (state: EditorState) => {
return text return text
} }
function findAlignment(cell: Node): string | null {
const alignment = cell.attrs.style as string
if (!alignment) {
return null
}
const match = alignment.match(/text-align: ?(left|right|center)/)
if (match && match[1]) {
return match[1]
}
return null
}
export const markdownSerializer = new MarkdownSerializer( export const markdownSerializer = new MarkdownSerializer(
{ {
...defaultMarkdownSerializer.nodes, ...defaultMarkdownSerializer.nodes,
image(state, node) { image(state: MarkdownSerializerState, node: Node) {
const alt = state.esc(node.attrs.alt || '') const alt = state.esc(node.attrs.alt || '')
const src = node.attrs.path ?? node.attrs.src const src = node.attrs.path ?? node.attrs.src
const title = node.attrs.title ? state.quote(node.attrs.title) : undefined const title = node.attrs.title ? `"${node.attrs.title}"` : undefined
state.write(`![${alt}](${src}${title ? ' ' + title : ''})\n`) state.write(`![${alt}](${src}${title ? ' ' + title : ''})\n`)
/* ![<alt-text>](<src-url> "<title>") */
}, },
code_block(state, node) { code_block(state, node) {
const src = node.attrs.params.src const src = node.attrs.params.src
@ -88,20 +104,6 @@ export const markdownSerializer = new MarkdownSerializer(
return findAlignment(cell) return findAlignment(cell)
} }
function findAlignment(cell: Node): string | null {
const alignment = cell.attrs.style as string
if (!alignment) {
return null
}
const match = alignment.match(/text-align:[ ]?(left|right|center)/)
if (match && match[1]) {
return match[1]
}
return null
}
node.forEach((table_child) => { node.forEach((table_child) => {
if (table_child.type.name === 'table_head') serializeTableHead(table_child) if (table_child.type.name === 'table_head') serializeTableHead(table_child)
if (table_child.type.name === 'table_body') serializeTableBody(table_child) if (table_child.type.name === 'table_body') serializeTableBody(table_child)
@ -122,9 +124,10 @@ export const markdownSerializer = new MarkdownSerializer(
} }
) )
function listIsTight(tokens: any, i: number) { function listIsTight(tokens: any, idx: number) {
let i = idx
while (++i < tokens.length) { while (++i < tokens.length) {
if (tokens[i].type != 'list_item_open') return tokens[i].hidden if (tokens[i].type !== 'list_item_open') return tokens[i].hidden
} }
return false return false
} }

View File

@ -1,6 +1,6 @@
import { Plugin, EditorState } from 'prosemirror-state' import { Plugin, EditorState } from 'prosemirror-state'
import { Node, Schema, SchemaSpec } from 'prosemirror-model' import type { Node, Schema, SchemaSpec } from 'prosemirror-model'
import { Decoration, EditorView, NodeView } from 'prosemirror-view' import type { Decoration, EditorView, NodeView } from 'prosemirror-view'
export interface ProseMirrorExtension { export interface ProseMirrorExtension {
schema?: (prev: SchemaSpec) => SchemaSpec; schema?: (prev: SchemaSpec) => SchemaSpec;
@ -21,7 +21,7 @@ export const isInitialized = (state: any) => state !== undefined && state instan
export const isEmpty = (state: any) => export const isEmpty = (state: any) =>
!isInitialized(state) || !isInitialized(state) ||
(state.doc.childCount == 1 && (state.doc.childCount === 1 &&
!state.doc.firstChild.type.spec.code && !state.doc.firstChild.type.spec.code &&
state.doc.firstChild.isTextblock && state.doc.firstChild.isTextblock &&
state.doc.firstChild.content.size == 0) state.doc.firstChild.content.size === 0)

View File

@ -1,4 +1,4 @@
import { EditorState } from 'prosemirror-state' import type { EditorState } from 'prosemirror-state'
import { serialize } from './markdown' import { serialize } from './markdown'
export const copy = async (text: string): Promise<void> => { export const copy = async (text: string): Promise<void> => {

View File

@ -63,6 +63,7 @@ export interface Collab {
export type LoadingType = 'loading' | 'initialized' export type LoadingType = 'loading' | 'initialized'
export interface State { export interface State {
isMac?: boolean
text?: ProseMirrorState; text?: ProseMirrorState;
editorView?: EditorView; editorView?: EditorView;
extensions?: ProseMirrorExtension[]; extensions?: ProseMirrorExtension[];

View File

@ -2,8 +2,8 @@ import { ClientOptions, dedupExchange, fetchExchange, createClient, Exchange } f
import { devtoolsExchange } from '@urql/devtools' import { devtoolsExchange } from '@urql/devtools'
import { isDev } from '../utils/config' import { isDev } from '../utils/config'
// export const baseUrl = 'https://newapi.discours.io' export const baseUrl = 'https://newapi.discours.io'
export const baseUrl = 'http://localhost:8000' // export const baseUrl = 'http://localhost:8000'
const exchanges: Exchange[] = [dedupExchange, fetchExchange] const exchanges: Exchange[] = [dedupExchange, fetchExchange]