Skip to content

Instantly share code, notes, and snippets.

@diatoming
diatoming / MacEditorTextView.swift
Created July 19, 2024 06:50 — forked from byte-sourcerer/MacEditorTextView.swift
An NSTextView wrapped by SwiftUI with TextKit 2
/**
* MacEditorTextView
* Copyright (c) Thiago Holanda 2020-2021
* https://twitter.com/tholanda
*
* MIT license
* Modified by https://github.com/cjwcommuny for TextKit 2
*/
import Combine
@diatoming
diatoming / View+BindingSync.swift
Created July 16, 2024 04:25 — forked from yannxou/View+BindingSync.swift
SwiftUI: Synchronize bindings
import SwiftUI
extension View {
/// Synchronizes the bindings so changes in any of them are propagated to the other.
///
/// The following example shows how to synchronize a Published property in a view model with a Binding used for presenting a sheet:
/// ```
/// class ViewModel: ObservableObject {
/// @Published var isPresented = false
@diatoming
diatoming / TextKit2.swift
Created July 14, 2024 23:40 — forked from krzyzanowskim/TextKit2.swift
Minimal setup of TextKit2 components to produce PDF document
import Cocoa
let loremIpsum = """
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque erat risus, laoreet et eros id, lobortis elementum sem. Nullam nec magna massa. Donec gravida felis at odio tincidunt pretium et in orci. Curabitur suscipit porta purus non faucibus. Cras dapibus felis eu enim rutrum, vel aliquet lacus volutpat. Quisque fermentum vulputate dictum. Morbi mattis orci quis dui posuere, id vulputate turpis tempus. Sed vulputate augue at ullamcorper feugiat. Phasellus nec lacus eget sapien congue lacinia vel et nulla. Phasellus sollicitudin gravida dapibus. Donec molestie ullamcorper lacus eu rutrum. Praesent lacinia dignissim dui eu ultricies. Integer in lacus lobortis, porttitor sem eu, tincidunt dolor. Cras dignissim nisl in maximus ullamcorper.
Proin nec vulputate magna. In interdum leo sit amet arcu consequat facilisis. Fusce ut malesuada ante, nec malesuada nisl. Mauris porta velit quis tortor mollis, in suscipit augue efficitur. Praesent eleifend mollis neque, non sempe
@diatoming
diatoming / README.md
Created July 3, 2021 23:06 — forked from IsaacXen/README.md
(Almost) Every WWDC videos download links for aria2c.
/*
Erica Sadun, http://ericasadun.com
Cross Platform Defines
Apple Platforms Only
Will update to #if canImport() when available
*/
@diatoming
diatoming / swift-responder-chain.swift
Created November 13, 2018 13:30 — forked from anandabits/swift-responder-chain.swift
A minimalist responder chain implemented in pure Swift
// Created by Matthew Johnson on 5/28/16.
// Copyright © 2016 Anandabits LLC. All rights reserved.
//
// This is a minimalist implementation of a responder chain in pure Swift.
//
// It is not intended to demonstrate the best way to
// implement event processing in Swift.
//
// The intent is to show how little code is necessary to acheive behavior
// similar to Cocoa's responder chain in pure Swift.
@diatoming
diatoming / install-docker-deb9.sh
Created August 20, 2018 03:52 — forked from upbeta01/install-docker-deb9.sh
Install Docker In Debian 9 (Stretch)
#!/bin/bash
#
# -----------------------
#
# This is a script that installs docker-ce (Docker Community Edition) on Debian 9
# Inspired by https://gist.github.com/frgomes/a6f889583860f5b330c06c8b46fa0f42
#
# -----------------------
# Pre-requesite
@diatoming
diatoming / gist:1cb4755e7d550abc0d1547062dd4485b
Created July 2, 2018 23:33 — forked from gleuch/gist:2475825
Javascript documentfragment to string (w/ text selection)
// selection range
var range = window.getSelection().getRangeAt(0);
// plain text of selected range (if you want it w/o html)
var text = window.getSelection();
// document fragment with html for selection
var fragment = range.cloneContents();
// make new element, insert document fragment, then get innerHTML!
@diatoming
diatoming / getTextNodesBetween.js
Created July 2, 2018 03:46 — forked from chriszarate/getTextNodesBetween.js
Native JavaScript function to get all *text* nodes contained in a selection object.
// Get all *text* nodes contained in a selection object.
// Adapted from code by Tim Down.
// http://stackoverflow.com/questions/4398526/how-can-i-find-all-text-nodes-between-to-element-nodes-with-javascript-jquery
function getTextNodesBetween(selection) {
var range = selection.getRangeAt(0), rootNode = range.commonAncestorContainer,
startNode = range.startContainer, endNode = range.endContainer,
startOffset = range.startOffset, endOffset = range.endOffset,
pastStartNode = false, reachedEndNode = false, textNodes = [];
function getTextNodes(node) {
var val = node.nodeValue;
@diatoming
diatoming / animatedScrollTo.js
Created June 28, 2018 23:36 — forked from joshbeckman/animatedScrollTo.js
ScrollTo animation using pure javascript and no jquery
document.getElementsByTagName('button')[0].onclick = function () {
scrollTo(document.body, 0, 1250);
}
function scrollTo(element, to, duration) {
var start = element.scrollTop,
change = to - start,
currentTime = 0,
increment = 20;