Skip to content

Instantly share code, notes, and snippets.

@JSoon
Forked from amk221/placeholder.css
Created February 14, 2025 06:34
Show Gist options
  • Save JSoon/bcbffb68c8666cb4e23e226600664795 to your computer and use it in GitHub Desktop.
Save JSoon/bcbffb68c8666cb4e23e226600664795 to your computer and use it in GitHub Desktop.

Revisions

  1. Andrew Kirwin revised this gist Sep 16, 2021. No changes.
  2. Andrew Kirwin revised this gist Sep 16, 2021. 2 changed files with 0 additions and 0 deletions.
    File renamed without changes.
    File renamed without changes.
  3. Andrew Kirwin revised this gist Sep 16, 2021. 4 changed files with 16 additions and 61 deletions.
    8 changes: 4 additions & 4 deletions placeholder-1.css
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    .ProseMirror__placeholder::after {
    color: grey;
    .ProseMirror[data-placeholder]::before {
    color: global.$placeholder-colour;
    position: absolute;
    content: attr(data-placeholder);
    pointer-events: none
    }
    pointer-events: none;
    }
    29 changes: 12 additions & 17 deletions placeholder-1.js
    Original file line number Diff line number Diff line change
    @@ -1,24 +1,19 @@
    import { Plugin } from 'prosemirror-state';
    import { Decoration, DecorationSet } from 'prosemirror-view';

    export default function placeholderPlugin(text) {
    return new Plugin({
    props: {
    decorations(state) {
    const doc = state.doc;
    const hasNoChildren = doc.childCount === 0;
    const isEmptyTextBlock =
    doc.childCount === 1 && doc.firstChild.isTextblock && doc.firstChild.content.size === 0;
    export default function placeholder(text) {
    const update = (view) => {
    if (view.state.doc.textContent) {
    view.dom.removeAttribute('data-placeholder');
    } else {
    view.dom.setAttribute('data-placeholder', text);
    }
    };

    if (hasNoChildren || isEmptyTextBlock) {
    const position = doc.inlineContent ? 0 : 1;
    const placeholder = document.createElement('span');
    placeholder.classList.add('ProseMirror__placeholder');
    placeholder.setAttribute('data-placeholder', text);
    return new Plugin({
    view(view) {
    update(view);

    return DecorationSet.create(doc, [Decoration.widget(position, placeholder)]);
    }
    }
    return { update };
    }
    });
    }
    6 changes: 0 additions & 6 deletions placeholder-2.css
    Original file line number Diff line number Diff line change
    @@ -1,6 +0,0 @@
    .ProseMirror[data-placeholder]::before {
    color: grey;
    position: absolute;
    content: attr(data-placeholder);
    pointer-events: none;
    }
    34 changes: 0 additions & 34 deletions placeholder-2.js
    Original file line number Diff line number Diff line change
    @@ -1,34 +0,0 @@
    import { Plugin } from 'prosemirror-state';

    class PlaceholderPluginView {
    constructor(editor, text) {
    this.editor = editor;
    this.text = text;
    this._updatePlaceholder();
    }

    update() {
    this._updatePlaceholder();
    }

    _updatePlaceholder() {
    const doc = this.editor.state.doc;
    const hasNoChildren = doc.childCount === 0;
    const isEmptyTextBlock =
    doc.childCount === 1 && doc.firstChild.isTextblock && doc.firstChild.content.size === 0;

    if (hasNoChildren || isEmptyTextBlock) {
    this.editor.dom.setAttribute('data-placeholder', this.text);
    } else {
    this.editor.dom.removeAttribute('data-placeholder');
    }
    }
    }

    export default function placeholder(text) {
    return new Plugin({
    view(editor) {
    return new PlaceholderPluginView(editor, text);
    }
    });
    }
  4. Andrew Kirwin revised this gist Apr 19, 2021. 2 changed files with 2 additions and 5 deletions.
    5 changes: 1 addition & 4 deletions placeholder-1.css
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,5 @@
    .ProseMirror__placeholder {
    color: grey;
    }

    .ProseMirror__placeholder::after {
    color: grey;
    position: absolute;
    content: attr(data-placeholder);
    pointer-events: none
    2 changes: 1 addition & 1 deletion placeholder-2.css
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    .ProseMirror[data-placeholder]::before {
    color: global.$placeholder-colour;
    color: grey;
    position: absolute;
    content: attr(data-placeholder);
    pointer-events: none;
  5. Andrew Kirwin renamed this gist Apr 19, 2021. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  6. Andrew Kirwin revised this gist Apr 19, 2021. 5 changed files with 49 additions and 19 deletions.
    34 changes: 34 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    import { Plugin } from 'prosemirror-state';

    class PlaceholderPluginView {
    constructor(editor, text) {
    this.editor = editor;
    this.text = text;
    this._updatePlaceholder();
    }

    update() {
    this._updatePlaceholder();
    }

    _updatePlaceholder() {
    const doc = this.editor.state.doc;
    const hasNoChildren = doc.childCount === 0;
    const isEmptyTextBlock =
    doc.childCount === 1 && doc.firstChild.isTextblock && doc.firstChild.content.size === 0;

    if (hasNoChildren || isEmptyTextBlock) {
    this.editor.dom.setAttribute('data-placeholder', this.text);
    } else {
    this.editor.dom.removeAttribute('data-placeholder');
    }
    }
    }

    export default function placeholder(text) {
    return new Plugin({
    view(editor) {
    return new PlaceholderPluginView(editor, text);
    }
    });
    }
    9 changes: 9 additions & 0 deletions placeholder-1.css
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    .ProseMirror__placeholder {
    color: grey;
    }

    .ProseMirror__placeholder::after {
    position: absolute;
    content: attr(data-placeholder);
    pointer-events: none
    }
    4 changes: 0 additions & 4 deletions placeholder.js → placeholder-1.js
    Original file line number Diff line number Diff line change
    @@ -1,10 +1,6 @@
    import { Plugin } from 'prosemirror-state';
    import { Decoration, DecorationSet } from 'prosemirror-view';

    /**
    * Placeholder plugin for prosemirror that supports documents with an inline, or a block schema.
    */

    export default function placeholderPlugin(text) {
    return new Plugin({
    props: {
    6 changes: 6 additions & 0 deletions placeholder-2.css
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    .ProseMirror[data-placeholder]::before {
    color: global.$placeholder-colour;
    position: absolute;
    content: attr(data-placeholder);
    pointer-events: none;
    }
    15 changes: 0 additions & 15 deletions placeholder.css
    Original file line number Diff line number Diff line change
    @@ -1,15 +0,0 @@
    /**
    * Pseudo content is used, positioned absolutely. Because otherwise, when clicking into the editor,
    * the cursor would be placed at the _end_ of the placeholder text, and then flicker to the beginning
    * as prosemirror moves the decoration to the correct side.
    */

    .ProseMirror__placeholder {
    color: grey;
    }

    .ProseMirror__placeholder::after {
    position: absolute;
    content: attr(data-placeholder);
    pointer-events: none
    }
  7. Andrew Kirwin revised this gist Apr 19, 2021. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion placeholder.css
    Original file line number Diff line number Diff line change
    @@ -11,4 +11,5 @@
    .ProseMirror__placeholder::after {
    position: absolute;
    content: attr(data-placeholder);
    }
    pointer-events: none
    }
  8. Andrew Kirwin revised this gist Apr 16, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion placeholder.js
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@ import { Plugin } from 'prosemirror-state';
    import { Decoration, DecorationSet } from 'prosemirror-view';

    /**
    * Placeholder plugin for prosemirror that supports documents with an inline only schema, or a block schema.
    * Placeholder plugin for prosemirror that supports documents with an inline, or a block schema.
    */

    export default function placeholderPlugin(text) {
  9. Andrew Kirwin revised this gist Apr 16, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion placeholder.css
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    /**
    * Pseudo content is used, positioned absolutely. Because otherwise, when clicking in to the editor,
    * Pseudo content is used, positioned absolutely. Because otherwise, when clicking into the editor,
    * the cursor would be placed at the _end_ of the placeholder text, and then flicker to the beginning
    * as prosemirror moves the decoration to the correct side.
    */
  10. Andrew Kirwin created this gist Apr 16, 2021.
    14 changes: 14 additions & 0 deletions placeholder.css
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    /**
    * Pseudo content is used, positioned absolutely. Because otherwise, when clicking in to the editor,
    * the cursor would be placed at the _end_ of the placeholder text, and then flicker to the beginning
    * as prosemirror moves the decoration to the correct side.
    */

    .ProseMirror__placeholder {
    color: grey;
    }

    .ProseMirror__placeholder::after {
    position: absolute;
    content: attr(data-placeholder);
    }
    28 changes: 28 additions & 0 deletions placeholder.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    import { Plugin } from 'prosemirror-state';
    import { Decoration, DecorationSet } from 'prosemirror-view';

    /**
    * Placeholder plugin for prosemirror that supports documents with an inline only schema, or a block schema.
    */

    export default function placeholderPlugin(text) {
    return new Plugin({
    props: {
    decorations(state) {
    const doc = state.doc;
    const hasNoChildren = doc.childCount === 0;
    const isEmptyTextBlock =
    doc.childCount === 1 && doc.firstChild.isTextblock && doc.firstChild.content.size === 0;

    if (hasNoChildren || isEmptyTextBlock) {
    const position = doc.inlineContent ? 0 : 1;
    const placeholder = document.createElement('span');
    placeholder.classList.add('ProseMirror__placeholder');
    placeholder.setAttribute('data-placeholder', text);

    return DecorationSet.create(doc, [Decoration.widget(position, placeholder)]);
    }
    }
    }
    });
    }