Guest User

Untitled

a guest
Jul 8th, 2025
19
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 4.31 KB | Source Code | 0 0
  1. import { Decoration, WidgetType, ViewPlugin } from '@codemirror/view';
  2. import { RangeSetBuilder } from '@codemirror/state';
  3.  
  4. export default (context) => {
  5.     return {
  6.         plugin: async (codeMirrorWrapper) => {
  7.             // Defines a CodeMirror view plugin that updates decorations on relevant changes
  8.             const PINPlugin = ViewPlugin.fromClass(
  9.                 class {
  10.                     constructor(view) {
  11.                         this.userValidated = false;
  12.                         this.decorations = applyDecorations(view, this);
  13.                     }
  14.                     update(update) {
  15.                         if (update.docChanged || update.viewportChanged || update.selectionSet) {
  16.                             this.decorations = applyDecorations(update.view, this);
  17.                         }
  18.                     }
  19.                 },
  20.                 {
  21.                     decorations: v => v.decorations
  22.                 }
  23.             );
  24.  
  25.             function applyDecorations(view, plugin) {
  26.                 const builder = new RangeSetBuilder();
  27.                 if (plugin.userValidated) {
  28.                     return builder.finish()
  29.                 }
  30.                 builder.add(
  31.                     0,
  32.                     0,
  33.                     Decoration.widget({
  34.                         widget: new PINOverlayWidget(view, plugin),
  35.                                         side: -1, // Make sure it comes before content
  36.                     })
  37.                 );
  38.                 return builder.finish();
  39.             }
  40.  
  41.             function validatePIN(pin, view, plugin) {
  42.                 console.error('PIN attempt:', pin);
  43.                 if (pin === '1234') { // Same as my luggage
  44.                     console.error('Successfully validated!');
  45.                     plugin.userValidated = true;
  46.                     plugin.decorations = applyDecorations(view, plugin);
  47.                     view.dispatch({ effects: [] });
  48.                 } else {
  49.                     // wrong PIN
  50.                 }
  51.             }
  52.  
  53.             class PINOverlayWidget extends WidgetType {
  54.                 constructor(view, plugin) {
  55.                     super();
  56.                     this.view = view;
  57.                     this.plugin = plugin;
  58.                 }
  59.  
  60.                 toDOM() {
  61.                     const overlay = document.createElement('div');
  62.                     overlay.style.position = 'absolute';
  63.                     overlay.style.top = '0';
  64.                     overlay.style.left = '0';
  65.                     overlay.style.width = '100%';
  66.                     overlay.style.height = '100%';
  67.                     overlay.style.background = 'white';
  68.                     overlay.style.zIndex = '1000';
  69.                     overlay.style.display = 'flex';
  70.                     overlay.style.flexDirection = 'column';
  71.                     overlay.style.alignItems = 'center';
  72.                     overlay.style.justifyContent = 'center';
  73.                     overlay.style.fontSize = '1.5em';
  74.                     overlay.style.gap = '1em';
  75.  
  76.                     const title = document.createElement('div');
  77.                     title.textContent = '🔒 Enter PIN to unlock';
  78.  
  79.                     const input = document.createElement('input');
  80.                     input.type = 'password';
  81.                     input.placeholder = 'PIN';
  82.                     input.style.fontSize = '1em';
  83.                     input.style.padding = '0.5em';
  84.  
  85.                     const button = document.createElement('button');
  86.                     button.textContent = 'Unlock';
  87.                     button.style.fontSize = '1em';
  88.                     button.style.padding = '0.5em 1em';
  89.  
  90.                     button.onclick = () => {
  91.                         const pin = input.value;
  92.                         input.value = '';
  93.                         validatePIN(pin, this.view, this.plugin);
  94.                     };
  95.  
  96.                     overlay.appendChild(title);
  97.                     overlay.appendChild(input);
  98.                     overlay.appendChild(button);
  99.  
  100.                     return overlay;
  101.                 }
  102.  
  103.                 ignoreEvent() {
  104.                     return true;
  105.                 }
  106.             }
  107.             // Add the extension
  108.             codeMirrorWrapper.addExtension(PINPlugin);
  109.         }
  110.     };
  111. };
  112.  
Advertisement
Add Comment
Please, Sign In to add comment